Skip Navigation

[Resolved] Exclude children terms only of a specific post type from parent taxonomy archive

This support ticket is created 7 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Thomas AMX 7 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#483880

Hi guys,

I have discovered that in some of my taxonomy archives the first page(s) display an incorrect number of items – posts are simply missing on the first page(s) and moved to the subsequent pages. In one archive I have a completely empty first page.

I have analyzed it and I understand now what happens and why it happens, but I don’t know how to solve it. The taxonomy is hierarchical, called Destinations: “Paris” is child of “France”, “France” is a child of “Europe” and so on. The archive contains two post types: custom post “Destination” and regular posts. The taxonomy is shared between these two post types.

The archive displays first 1 Custom Post: for example “Paris” and then all regular posts having this term. I used a custom function to sort the archive by post type and it works. I also used conditionals to match the destination types. For example in the archive “France” or “Europe” the custom post “Paris” doesn’t appear, because it isn’t foreseen by the conditionals.

And I had thought so, until I discovered the pagination issue. All Custom Posts that are Children Terms of “Europe” actually appear on the first pages, it is just that according to the Loop Design their content is not displayed. It’s like they’re invisible or empty but they are there and influence the pagination.

How can I exclude children terms of a certain post type from the archive? For example, if the archive is “Europe” I need to display only 1 Custom Post “Europe” and afterwards all regular posts that have children terms of Europe: “France”, "Italy", “Paris”, “Rome”, “London”, etc.

I found this, so I suppose it possible:

http://wordpress.stackexchange.com/questions/169673/hide-child-term-posts-on-parent-term-pages

and this:

https://toolset.com/forums/topic/exclude-taxonomy-and-children/

But the question is how can I adapt it and apply it only for a specific custom post type? Or maybe there is another way? Regular posts should be unaffected and such posts with children terms should appear in the archives of their parent terms.

Please help.

Kind regards,
Tom

#484035

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - using pre_get_posts you can set the specific post type which you can query.

For example:

function func_specific_posttype( $query ) {
   if( $query->is_tax( 'company-category' ) && $query->is_main_query() && !is_admin() ) {
        $query->set( 'post_type', 'projects' );
   }
}
add_action( 'pre_get_posts', 'func_specific_posttype' );
#484106

Hello Minesh,

Thanks, but I think we're not on the same page.

What I need to achieve:

1. Archive of a hierarchical taxonomy
2. Two post types sharing the same taxonomy must appear in the archive
3. Regular posts having a child term of the current term should appear in the archive.
4. Custom posts having a child term of the current term should be excluded from this archive. If it's a custom post, only posts with exactly the same taxonomy term should appear.

There are some threads about excluding posts with children terms, but I need to add a condition that the exclusion applies to custom posts only.

Thank you.
Tom

#484219

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Basically - its not supported natively to not to include posts that belongs to child term. Could you please share temporary access details and problem URL with test case. I will try to check and see if its possible or not.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#484378

Hello Minesh,

Sure, I can set up a sandbox clone of the site and provide you access. But honestly, I don’t suppose it will be helpful or productive to try to analyze the structure of the site, when the nature of the issue is generic and not site-specific. This is an abstract issue, and not a troubleshooting case.

There is no “problem” as such, because the website is working and Toolset does what it was programmed to do. I am just looking for a small tweak that will modify the default behavior of taxonomy archives. There is nothing to troubleshoot. Deactivating plugins/themes will NOT change absolutely anything, because there is no error. All I need is to slightly modify the default behavior of archives.

I am aware that by default, posts with children terms are displayed in parent term’s archives. But I also know that this behavior can be changed by a function or filter, and there are some examples in the Toolset forums and elsewhere. There is a native WordPress taxonomy parameter “include_children“, which has the default value “true” but it can be changed.

What I am looking for is such a function or filter, setting “include_children” to false, but with the additional condition of “post type”. It should be applied to a selected post type and not to others. Unfortunately, I don’t know php and cannot achieve it on my own.

As I said, this is an abstract issue and not site-specific. No troubleshooting is necessary because there is no error as such. A possible solution would be a function/filter switching “include_children” to false in specified conditions and it would be applicable to all sites.

If you could suggest such a function/filter, I would be very grateful and eager to test it.

Kind regards
Tom

#484409

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please find below code which I tested with custom taxonomy called "hobby" which is assigned to default post type "post" and custom post type "student". You should change the relative field with below code.

add_action( 'pre_get_posts', 'cyb_pre_get_posts' );
function cyb_pre_get_posts( $query ) {
	
	if( $query->is_tax( 'hobby' ) && $query->is_main_query() && !is_admin() ) {
		
		$children =	 get_term_by( 'slug', $query->query_vars['hobby'], 'hobby' );
			if( $children->parent == 0 ) {
	
					/// find child post entries
					$posts_array = get_posts(array(
						'posts_per_page' => -1,
						'post_type' => 'student',
						'fields' => 'ids',
						'tax_query' => array(
							array(
								'taxonomy' => 'hobby',
								'field' => 'slug',
								'terms' => $query->query_vars['hobby'],
								'include_children' => false
							))));

					$posts_post = get_posts(array(
						'posts_per_page' => -1,
						'post_type' => 'post',
						'fields' => 'ids',
						'tax_query' => array(
							array(
								'taxonomy' => 'hobby',
								'field' => 'slug',
								'terms' => $query->query_vars['hobby'],
								'include_children' => true
							 ))));
					
					
					$query->query_vars['post__in'] = array_merge($posts_array,$posts_post);
			}
   
	}
}
#484787

Minesh,

Your function works like a charm. Thank you so much. You saved my day.

Best regards,
Another happy Toolset customer - Tom

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