Skip Navigation

[Resolved] Populate relationship (M2M) custom fields with parent/child titles

This support ticket is created 2 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by jorgel-4 2 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#2331201

Hello!

1) I have two CPTs: Services and Locations
2) With a many-to-many relationship: Services-Locations
3) And 2 relationship custom fields: ¨Type of Service¨ and ¨Location¨

---> I need to populate:
- ¨Type of Service¨ field with title from parent CPT Services
- ¨Location¨ field with title from parent CPT Location

With the documentation and the forum I got to this but it doesn't work:

add_action( 'toolset_association_created', 'custom_intermediary_post_populate_fields', 10, 5 );
function custom_intermediary_post_populate_fields( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
if ($association_slug == 'servicio-localidad') {

$parent_title = get_the_title($parent_id);
$child_title = get_the_title($child_id);

$updated_data = array(
'ID' => $intermediary_id,
'wpcf-servicio-post-intermedios' => $parent_title,
'wpcf-localidad-post-intermedios' => $child_title,
);
update_post_meta( $updated_data );
}
}

Note: I clarify that this function is not to show these fields in a view but to be able to export them in a cvs file

Thanks in advance

#2331345

Hi,

Thank you for contacting us and I'd be happy to assist.

I'll recommend a few changes to the code, for the part that updates the custom field values:


add_action( 'toolset_association_created', 'custom_intermediary_post_populate_fields', 10, 5 );
function custom_intermediary_post_populate_fields( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
	if ($association_slug == 'servicio-localidad') {

		$parent_title = get_the_title($parent_id);
		$child_title = get_the_title($child_id);

		update_post_meta( $intermediary_id, 'wpcf-servicio-post-intermedios', $parent_title );
		update_post_meta( $intermediary_id, 'wpcf-localidad-post-intermedios', $child_title );
	}
}

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2331711

Thanks for your help, but there isn't anything saved at the fields after the connection is created.

I think it could be because when I'm at a "Service" post and click at "create new location" to connect with or "connect with existing location", it opens a modal and the relation is saved with fields in blank.

I tried changing the priority argument at the function and also using the code directly at functions.php, but no results.

Is there a query that could run when the relation data is saved? In that case, will this work when I create the relations by importing a cvs too?

One last thing, is there a way to connect a new "Service" post with all available "Locations" by default?

#2333271

To suggest the next steps, I'll need to understand exactly how you're importing the posts and creating post-relationship connections between them.

Can you please share temporary admin login details, along with the detailed requirements and workflow?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2335047

Thank you for sharing this information.

Based on your workflow, I'll recommend registering a custom shortcode, which can cycle through all the intermediary posts, and then update its parent and child post titles, in the respective custom fields:


add_shortcode('update_intermediary_post_fields', 'update_intermediary_post_fields_func');
function update_intermediary_post_fields_func($atts) {
	// process shortcode attributes
	$intermediary_post_slug	= $atts['intermediary_post_slug'];
	$relationship_slug		= $atts['relationship_slug'];

	$field_for_parent_slug	= $atts['field_for_parent_slug'];
	$field_for_child_slug	= $atts['field_for_child_slug'];

	// get all available intermediary posts
	$args = array(
		'post_type'        => $intermediary_post_slug,
		'posts_per_page'   => -1,
		'post_status'      => 'publish',
	);
	$posts_array = get_posts( $args );

	// cycle through all intermediary posts
	foreach ($posts_array as $post) {
		// get the related parent and child posts of the current intermediary post
		$get_results = toolset_get_related_posts( $post->ID, $relationship_slug, 'intermediary', 1, 0, array(), 'post_object', 'all' );

		// update the titles of the parent and child posts, in the respective custom fields
		update_post_meta( $post->ID, $field_for_parent_slug, $get_results[0]['parent']->post_title );
		update_post_meta( $post->ID, $field_for_child_slug, $get_results[0]['child']->post_title );
	} 
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Once the relationship information has been imported and the intermediary posts have been created (but before exporting them in a CVS file), you can create a new temporary page and add this shortcode to it:


[update_intermediary_post_fields intermediary_post_slug="servicio-localidad" relationship_slug="servicio-localidad" field_for_parent_slug="wpcf-servicio-post-intermedios" field_for_child_slug="wpcf-localidad-post-intermedios"]

Please note how the shortcode attributes are used to pass the information about the slugs of intermediary post type, relationship, and the fields for the parent and child post titles.

Visit the page on the front-end once and a shortcode will run in the background to update the custom field values and you'll be able to export the intermediary post data.

As for connecting a new "Service" post with all available "Locations" by default, there is no built-in feature available for this, so you'll need another custom shortcode similar to the one shared above to do this processing, programmatically.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2335153

It works perfectly, thank you very much!

I will try by my self first, to make a function for that using your code as reference. Is there a specific documentation you can suggest me to work with?