Skip Navigation

[Resuelto] Filtering children by child and parent field on the same page

This support ticket is created hace 3 años. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Autor
Mensajes
#2020459
relationship.PNG

Good Morning,

I'm Sebastian, I work in a company (Avancem Grup) as a web developer. And I have a problem about toolset (relationship) and I wanted to know about the code to solve it.

Tell us what you are trying to do?
I have a page where a list of products (children) is shown and I was trying to filter by the fields of (parent and child) on the same page. Filter fields: PRODUCT NAME, PRODUCT CATEGORY, FLORIST NAME, FLORIST ADDRESS,

Is there any documentation that you are following?

Is there a similar example that we can see?
I Attached a screenshot of what I wanted to do

What is the link to your site?
Right now this page is filtering only the name and product category. And I wanted to add a filter of a parent field.
hidden link

Thank you in advance.

Best regards,

#2020537

Waqar
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Sebastian,

It seems that our chat was disconnected earlier.

For this particular requirement, unfortunately, we don't have any alternative or workaround, except for the ones that were already discussed in that chat.
( ref: https://toolset.com/forums/topic/toolset-chat-support-ticket-by-johan-sebastianr-2-1618386465/ )

In summary, you can:

1. Use two separate views with their separate search forms, one for florist posts and the other for the product posts.

OR

2. Through the post-relationship filter the florist post titles can be added as a select type input field in the view for the products. However, for the custom field like an address that is only attached to the florist posts, you'll have to save them as duplicates with the product posts too.

Please let me know if you have any follow-up questions.

regards,
Waqar

#2020593

Hello,

How we duplicate the value of the address field with address field child. And when the value of the parent field is changed, the child will also change.

Best regards,

#2020621

Hello,

What we are worrying about must be controlled every change of the address field in the parent.

Best regards,

#2020631

Hello,

What we are worrying about must be controlled every change of the address field in the parent.

#2021847

Waqar
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

To keep the location custom fields between the parent and child posts in sync, you can use some custom code.

For example, to make sure that when a relationship connection is created between a parent and child post, the parent post's address field value is copied to the child's address field value, you can use the "toolset_association_created" hook:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created

Example:

Suppose that the slug of the location field is "location-address" in both the parent and child post types and the slug of the target relationship is "shop-book".


add_action( 'toolset_association_created', 'update_location_field_association_created', 10, 5 );
function update_location_field_association_created( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
	$field_slug = 'location-address';
	$target_association = 'shop-book';

	if ($target_association == $association_slug ) {
		$parent_location = types_render_field( $field_slug, array( 'item' => $parent_id, 'output' => 'raw' ) );
		update_post_meta( $child_id, 'wpcf-'.$field_slug, $parent_location );
	}
}

Likewise, to make sure that when the parent or child post is updated from the admin area, the parent post's location information is copied to all the connected child posts, you can use "save_post" hook with "toolset_get_related_posts" function:
https://developer.wordpress.org/reference/hooks/save_post/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Example:

Suppose that the slug of the location field is "location-address" in both the parent and child post types and the slug of the target relationship is "shop-book".

And the slug of the parent post is "shop" and the slug of the child post is "book".


function update_location_field_on_save( $post_id, $post ){
	$parent_post_slug = 'shop';
	$child_post_slug = 'book';
	$target_association = 'shop-book';
	$field_slug = 'location-address';

	if ( ($parent_post_slug == $post->post_type) && ($_POST['action'] =='editpost') ) {

		$parent_location = types_render_field( $field_slug, array( 'item' => $post_id, 'output' => 'raw' ) );
		$get_related_children = toolset_get_related_posts( $post_id, $target_association, 'parent', 999999, 0, array(), 'post_id', 'child' );

		foreach ($get_related_children as $get_result ) {
			update_post_meta( $get_result, 'wpcf-'.$field_slug, $parent_location );
		}

	}
	else if ( ($child_post_slug == $post->post_type) && ($_POST['action'] =='editpost') )
	{
		$get_related_parent = toolset_get_related_posts( $post_id, $target_association, 'child', 1, 0, array(), 'post_id', 'parent' );

		foreach ($get_related_parent as $get_result ) {
			$parent_location = types_render_field( $field_slug, array( 'item' => $get_result, 'output' => 'raw' ) );
			update_post_meta( $post_id, 'wpcf-'.$field_slug, $parent_location );
		}
	}
}
add_action( 'save_post', 'update_location_field_on_save', 30, 2 );

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.

Important 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/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.