Skip Navigation

[Resolved] Create search filter using a child taxonomy (category) to display parent results

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to filter a custom post type by the taxonomy of a related(child) post type in a Toolset relationship.

Solution:
That's not possible out of the box. We cannot filter posts by taxonomies or custom fields from a related post type.

However, we can sync the taxonomies from the child post to the parent post and use it there. Check the code that the user was using to sync the taxonomies when a child post is submitted.
https://toolset.com/forums/topic/create-search-filter-using-a-child-taxonomy-category-to-display-parent-results/#post-2012133

Relevant Documentation:

This support ticket is created 3 years, 7 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: Africa/Casablanca (GMT+01:00)

This topic contains 7 replies, has 2 voices.

Last updated by anthonyD-3 3 years, 7 months ago.

Assisted by: Jamal.

Author
Posts
#2008383
Screen Shot 2021-04-02 at 8.35.56 AM.png

Tell us what you are trying to do?
Create a search filter using a child taxonomy (category) to display parent results.
For example:
We have a parent post type of Person
We have a child post type of Review
Reviews belong to one of four categories.

I would like to build a search page where you can search by Person name
As well as filter the results based on one of the four child categories of the Review
The idea being, you can search for Person by name or filter to find Person based on a selected associated Review Category

Is there any documentation that you are following?
No

Is there a similar example that we can see?
Not that I could find.

What is the link to your site?
This is a private site.

#2008533

Hello and thank you for contacting the Toolset support.

Currently, it is not possible to filter a post view using taxonomies or custom fields from a related post(parent, child, or intermediary). You will need to have the taxonomy assigned to the Person post too to be able to filter with it.

However, I can imagine a couple of scenarios where this can be implemented. It will require some custom code:
1. Changing the view's query filter to search within certain posts using the post__in wp_query argument. You will need to get the reviews posts that are assigned to the selected category. Use their IDs to get the parent posts using the Toolset Relationships API, especially the toolset_get_related_posts. And pass them to the post__in parameter of the view's query arguments.
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
- https://developer.wordpress.org/reference/functions/get_posts/
- https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

2. Instead of hooking to the view's query, synchronize the category term to the parent post(Person) when a child post(Review) is assigned. Assuming the reviews are created using a Toolset form, you can hook into the cred_save_data hook and assign the same category term to the parent Person post
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

We do have plans to offer more sophisticated search options, such as searching with taxonomies and custom fields of related posts, or searching with more than one relationship, but I am afraid this may wait for the next year.

I hope this helps. Let me know if you have any questions.

#2010749

Thank you for the quick response Jamal.

I like the second option as it appears simple enough to implement. I am not a developer though, would you happen to have any code examples of how to implement? We are using a Toolset form to create the evaluations.

#2010815

There are examples of usage of each of our filters and actions below their documentation toggled by the button "More Usage examples". Check this screenshot hidden link
You can also perform a scoped Google search on the API that you will be using cred_save_data and toolset_get_related_post like this hidden link

This is an example of code that is trying to do the inverse, copy the taxonomy terms from the parent post to the child post https://toolset.com/forums/topic/set-value-of-child-post-taxonomy-form-field-by-the-current-page-post-taxonomy/#post-1438247
You just need to change lines 13-15.
Get the taxonomy terms(category) from the child post instead of the parent, and assign them to the parent post instead of the child

// get the categories from the child post(Review)
$child_terms = get_the_terms( $child_id, $tax );
// get the IDs of the terms
$child_term_ids = wp_list_pluck( $child_terms, 'term_id' );
// set the terms for the parent post(Person)
wp_set_post_terms( $parent_id, $child_term_ids, $tax, true );

Notice how I added "true" in the last line(calling wp_set_post_terms), this will add the term of review, to the person's terms without deleting the existing terms. https://developer.wordpress.org/reference/functions/wp_set_post_terms/

I hope this helps. Let me know if you have any questions.

#2012127

Thanks for the direction, this really help out.
Here is the final code snippet that worked for us... We had to change the action though to 'cred_submit_complete' as 'cred_save_data' fired too early for the snippet to work.


add_action( 'cred_submit_complete', 'nf_copy_taxonomy', 10, 2 );

function nf_copy_taxonomy( $child_id, $form_data ){

    /** Settings */
    $forms = array( ID ); // ID(s) of forms
    $tax = 'tax-slug'; // taxonomy slug
    $relationship = 'relationship-slug'; // slug of relationship

    if ( in_array( $form_data['id'], $forms ) ){

        $parent_id = toolset_get_related_post( $child_id, $relationship  );
        $child_terms = get_the_terms( $child_id, $tax );
        $child_term_ids = wp_list_pluck( $child_terms, 'term_id' );
        wp_set_post_terms( $parent_id, $child_term_ids, $tax, true );

    }
} 

#2012133

Reposting as I am not sure previous was successful.

Thanks for your direction. Here is the final code snippet that got it working for us. We had to use the action 'cred_submit_complete' instead of 'cred_save_data' because this fired too early in the process to get the parent_id. However changing to 'cred_submit_complete' fired late enough to retrieve parent_id.

Overview, Assigning child taxonomy to parent taxonomy on form submission.

add_action( 'cred_submit_complete', 'nf_copy_taxonomy', 10, 2 );

function nf_copy_taxonomy( $child_id, $form_data ){

    /** Settings **/
    $forms = array( ID ); // ID(s) of forms
    $tax = 'tax-slug'; // taxonomy slug
    $relationship = 'relationship-slug'; // slug of relationship

    if ( in_array( $form_data['id'], $forms ) ){

        $parent_id = toolset_get_related_post( $child_id, $relationship  );
        $child_terms = get_the_terms( $child_id, $tax );
        $child_term_ids = wp_list_pluck( $child_terms, 'term_id' );
        wp_set_post_terms( $parent_id, $child_term_ids, $tax, true );

    }
}
#2012195

Thank you for sharing your code. I am glad I could be of any help.

If you don't need any further assistance, kindly mark this ticket as resolved.

#2012199

My issue is resolved now. Thank you!