Skip Navigation

[Resolved] In custom search: When will posts with child terms of searched terms be found?

This support ticket is created 3 years 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+00:00)

This topic contains 6 replies, has 3 voices.

Last updated by a.R 3 years ago.

Assisted by: Jamal.

Author
Posts
#1995759

a.R

When searching for posts with a parent term: When will posts with the child terms be found?

e.g.
FRUIT
- APPLE
- BANANA

When will posts with BANANA (but WITHOUT fruit!) be found when searching for FRUIT?
We believe to have seen various behaviour in that point.

----
I HAVE ASKED THAT BEFORE, but the answer doesn´t match our experience:
E.g. in backend, we see 356 pages for "Methoden - Kommunizieren", but custom search gives us 578!
----

THANK YOU, kind regards,

Achim

#1996063

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

A search View filtered by taxonomy will automatically find posts that have child terms assigned, even when the parent that is being searched for is not assigned.

So filtering by Fruit will include any posts that have "Fruit", or "Apple", or "Banana" assigned in the results.

There are no options in the GUI to change that, but the 'include_children' option of the 'tax_query' attribute can be set to false by code that hooks into 'wpv_filter_query' (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query)

#2008543

a.R

Hi Nigel, thank you.
Does 'include_children' set this for ALL taxonomies in the view, or is there a way to set it PER taxonomy?

#2008561

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello! The 'include_children' needs to be set for the taxonomy that you want. Check an example code on this reply, it is applied only for the taxonomy "animal-group"
https://toolset.com/forums/topic/in-custom-search-when-will-posts-with-child-terms-of-searched-terms-be-found/#post-1968065

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

#2009009

a.R

Now I put this in my functions.php, and it gives me a FATAL ERROR:

	add_filter( 'wpv_filter_query', 'taxonomy_view_func', 10, 2 );
	function taxonomy_view_func( $query, $setting ) {
	    if ( $setting['view_id'] = 5522 ) {
	        if ( isset( $query['tax_query'] ) ) {
	            foreach ( $query['tax_query'] as $k => $v ) {
	                if(isset($v['taxonomy']) && $v['taxonomy'] == 'armethoden') {
	                    $query['tax_query'][$k]['include_children'] = false;
	                    break;
	                }
	            }
	        }
	    }
	    return $query;
	}

What´s wrong? Thank you, kind regards.

#2009017

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

A fatal error means almost probably a typo or syntax error, I could not spot it by eye. Maybe it is an issue with single quotes. Would you allow me FTP access and WordPress access to your website to check this further? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

Line 7 will also need to be changed to:

                    $query['tax_query'][$k]['include_children'] = true;

Please let me know where the view is used, so I can test it and see further. And let me know a test scenario, for example:
- If I choose the term xxx
- I would expect to see the post yyy that has the term zzz which is a child of term xxx.
This will help me faster than me guessing what outcome on your site which I am not familiar with.

#2018207

a.R

My issue is resolved now. Thank you!

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