Skip Navigation

[Resolved] Children + Parent Taxonomy on Searches

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

Problem:
The user would like to exclude the child terms, in a taxonomy filter and only display the parent ones. The user would like to do the above for a view and an archive template.

Solution:
For taxonomy filters in posts views, prefer to use the wpv_filter_taxonomy_frontend_search_get_terms_args hook. Use something like:


add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );
  
function prefix_modify_get_terms_args( $args, $taxonomy, $view_id ) {
    if ( $view_id == 1223 && $taxonomy == "topic") {
        $args['parent'] = 0;
    }
    return $args;
}

Whereas for taxonomy views, prefer the wpv_filter_taxonomy_query hook. Something like:

add_filter( 'wpv_filter_taxonomy_query', 'prefix_modify_taxonomy_view_args', 99, 3 );
function prefix_modify_taxonomy_view_args( $tax_args, $view_settings, $view_id ) {
    if ( $view_id == 1201 ) {
        $tax_args['parent'] == 0;
    }
    return $tax_args
}

Relevant Documentation:

This support ticket is created 3 years, 12 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 5 replies, has 2 voices.

Last updated by abbyJ-2 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#2092777
Screen Shot 2021-06-18 at 1.38.37 PM.png
Screen Shot 2021-06-18 at 1.38.33 PM.png

Tell us what you are trying to do? I'm trying to show only parent "Topics" taxonomy on my keywords search and parent and child "Topics" on my Issue search.

Is there any documentation that you are following? https://toolset.com/forums/topic/how-do-i-limit-my-dropdown-to-show-only-first-level-in-a-taxonomy/#post-1343875

Is there a similar example that we can see? I have only parent Topics currently showing under Categories>Topics on the keywords search, but it's the same on the Issue search. I want to show ALL topics on the Issue search. I currently have the code in the above topic forum in my functions.php file.

What is the link to your site? hidden link which has a Coming soon page. I'll need to give you logins to actually see the search. See uploaded pictures for quick look.

#2093221

Hello and thank you for contacting Toolset support.

Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

Please provide the URLs for:
- The keywords search.
- The Issues search.
The URLs will help me understand what context the page is in? Archive page, single post page, etc.

#2093761

Thank you for the credentials, I now see the issue more clearly.

First of all, the custom code that you used makes more sense for search filters in archive templates, while the keywords search is a posts view, and the issues search is actually a taxonomy view. Views, in the opposite of archive templates offers hooks that we can use instead of using the default WordPress hook pre_get_terms which will be run everywhere where taxonomy terms will be displayed(in views, and in archive templates)
https://developer.wordpress.org/reference/hooks/pre_get_terms/

For taxonomy filters in posts views, prefer to use the wpv_filter_taxonomy_frontend_search_get_terms_args hook. Something like:

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );
 
function prefix_modify_get_terms_args( $args, $taxonomy, $view_id ) {
    if ( $view_id == 1223 && $taxonomy == "topics") {
        $args['parent'] = 0;
    }
    return $args;
}

Read more about the hook here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_get_terms_args

Whereas for taxonomy views, prefer the wpv_filter_taxonomy_query hook. Something like:

add_filter( 'wpv_filter_taxonomy_query', 'prefix_modify_taxonomy_view_args', 99, 3 );
function prefix_modify_taxonomy_view_args( $tax_args, $view_settings, $view_id ) {
    if ( $view_id == 1201 ) {
        $tax_args['parent'] == 0;
    }
    return $tax_args
}

Read more about the hook here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query

Note that 1223 and 1201 are respectively the IDs of the posts view for keywords search, and the taxonomy view in the issues search.

Please note that I did not test these codes, primarily because I do not have FTP access to your website and any errors(even a typo) may crash the website.

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

#2096157

Hi Jamal,

The wpv_filter_taxonomy_frontend_search_get_terms_args hook works wonderfully, with just a minor fix.

My last silly question is, if I want to show just the parent taxomy on BOTH the topics and the formats, how do I add that to the hook:

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );

function prefix_modify_get_terms_args( $args, $taxonomy, $view_id ) {
if ( $view_id == 1223 && $taxonomy == "topic") {
$args['parent'] = 0;
}
return $args;
}

My php is quite rusty and I keep breaking the code when I try. Thank you SO much for your help!

#2098957

Hello again, my apologies for the late reply, but I do not work on Wednesdays and Thursdays.

You can add a condition on the Formats taxonomy too. Something like this should work:

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'prefix_modify_get_terms_args', 10, 3 );
  
function prefix_modify_get_terms_args( $args, $taxonomy, $view_id ) {
    if ( $view_id == 1223 && ( $taxonomy == "topic" || $taxonomy == "format" )) {
        $args['parent'] = 0;
    }
    return $args;
}

I updated it on the Toolset custom code snippet and it works. Check this screenshot hidden link

#2099279

Great, thank you Jamal! I appreciate your help! Everything is working perfectly now.