Skip Navigation

[Resolved] Create a view to limit results to a parent taxonomy then filter by child

This support ticket is created 2 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: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Matthew Mockford 2 years, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2174903

Tell us what you are trying to do?

I would like to create a view that only displays posts (custom post) of a parent taxonomy but then includes on page filters to filter by sub categories.

I'm going to use Genres for an example as I think its an easier way to explain than the working version.

Parent Taxonomy = Action
Child Taxonomy = Martial Arts, War & Military, Shoot 'Em Up

Parent Taxonomy = Drama
Child Taxonomy = Caper, Slapstick, Stand-up

In my example I want the two parent taxonomies to be completely separate and the results for each will be displayed on different pages.

I have it working to an extent by adding the following to pre_get_posts, the problem with this is. Should the user have made a mistake and added the post to categories in both parent taxonomies, the on page filter will display them.

function define_parent_query( $query ) {

global $WP_Views;

if ( $WP_Views->current_view == 21657 ) {

$taxquery = array(
array(
'taxonomy' => 'video-category',
'field' => 'slug',
'terms' => 'action',
'operator'=> 'IN'
)
);
$query->tax_query->queries[] = $taxquery;
$query->query_vars['tax_query'] = $query->tax_query->queries;

/* $query->set( 'tax_query', $taxquery ); */

}
}

I have also looked into using wpv_filter_query to exclude / include the specific categories but it either displays every post or none.

Is there any documentation that you are following?

https://toolset.com/forums/topic/filter-categories-pre_get_posts-not-working/

#2175707

Waqar
Supporter

Languages: English (English )

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

Hi,

Thank you for contacting us and I'd be happy to assist.

For what you're planning, you can use the "wpv_filter_query" filter:
( https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )

For example, suppose you have a post view with ID "123", where you only want to include the results under the parent term 'Action' which has the ID "456", in the taxonomy "category". The function, in this case, would look like this:


add_filter( 'wpv_filter_query', 'filter_include_cat_fn', 1000 , 3 );
function filter_include_cat_fn( $query_args, $view_settings ) {
	// check if specific view
	if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 123) ) {
		// set the parent tax term filter if no tax filter is set
		if(empty($query_args['tax_query']))
		{
			$query_args['tax_query'][0]['taxonomy'] = 'category';
			$query_args['tax_query'][0]['field'] = 'id';
			$query_args['tax_query'][0]['terms'][0] = 456;
			$query_args['tax_query'][0]['operator'] = 'IN';
			$query_args['tax_query'][0]['include_children'] = true;
			$query_args['tax_query']['relation'] = 'AND';
		}
	}

	return $query_args;
}

As a result, when the page will load, the view will bring in only the results which have "Action" or its child terms.

Important: In the view's custom search setting, you'll need to enable the option to show only those options, which can produce results. This way, only the child terms of the parent "Action" term will show in the front-end search form.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2181769

Thank you for your help. This is currently reproducing a similar result to what I had before (I have implemented your version).

The results of the search is working as required; its the filters that I am having problems with.

If a post has the category 'Martial Arts' (child of Action) & 'Caper' (child of Comedy). The 'Caper' category will appear as an option within the filters. This is what I would like to avoid. On the 'Action' page, I only ever want the child categories within the filters.

I have enabled the option to show only those options within the search. (Underneath 'Which options to display in the form inputs')

Many thanks,

#2182573

Waqar
Supporter

Languages: English (English )

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

Thanks for the update.

To limit the terms in the search filter, you can additionally use the filter "wpv_filter_taxonomy_frontend_search_get_terms_args".
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_get_terms_args )

For example, suppose you have a view with ID "123" and in it, you have a search filter for taxonomy 'book-category' and you'd like to include only the child terms for the parent term "history" with term ID "456". The code, in this case, would look like this:


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 ) {
	// target parent term ID
	$term_id = 456;
	// target taxonomy slug
	$taxonomy_name = 'book-category';
	// target view's ID
	$views = array( 123 );
	// check if specific view
	if ( in_array( $view_id, $views ) ){
		// get child terms of the target parent term
		$termchildren = get_term_children( $term_id, $taxonomy_name );
		// include only those child terms in search filter
		$args['include'] = $termchildren;
	}

	return $args;
}

Feel free to adjust this code snippet as per your requirements.

#2187929

Perfect, thank you.

#2187931

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.