Skip Navigation

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

This support ticket is created 3 years, 1 month 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.

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: Africa/Casablanca (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by Jamal 3 years ago.

Assisted by: Jamal.

Author
Posts
#1967683

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.

THANK YOU, kind regards,

Achim

#1968065

Jamal
Supporter

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

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

Hello and thank you for contacting the Toolset support.

By default, the view will look only for the posts with the searched/selected term. If you would like to search for posts with children terms you will need to alter the view's query arguments. This can be done by adding the include_children parameter to the tax query. Read more about it here https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

The custom code needs to hook to the wpv_filter_query hook.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Something like the following should work. You will need to use the view's ID on line 2 and the taxonomy slug on line 6

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

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

#1970943

a.R

My issue is resolved now. Thank you!

#2009007

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;
	}
#2009129

Jamal
Supporter

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

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

Hello Achim, I reopened this ticket to check if you finally got the custom code working or not. If yes, please mark this ticket as resolved. If no, allow me access to WordPress and FTP access to check this further. Please note that I won't touch the custom code without having FTP access in order to not break the site. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

I will also need to know where the view is used, and a test scenario, so I don't have to guess:
- Select term xxx in the following filter.
- I expect to get results yyy, because they have the child term zzz.

#2009441

a.R

So you don´t see any errors in my code?

#2009703

Jamal
Supporter

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

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

No, I don't see any error that should trigger a Fatal Error.

The topic ‘[Closed] In custom search: When will posts with child terms of searched terms be found?’ is closed to new replies.