Skip Navigation

[Resolved] Limit the output of an Archive for a taxonomy to only show a certain post type.

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

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 7 years ago.

Assisted by: Luo Yang.

Author
Posts
#596927
archive results no debug.JPG
archive results.JPG

I have a taxonomy shared between and Parent and Child post types. When I display the taxonomy archive for a single term it returns the child posts as well as the main posts. I want to limit the archive display to parent posts only.

I referred to this post: https://toolset.com/forums/topic/remove-cpt-from-categorycategory-slug-page-2/

which seems like exactly what I want to do but since the output is in a loop I can't quite figure out where to put the conditional.

If I wrap the Loop Item in the conditional I get gaps in the result display.

#597070

Dear EdG4627,

I suggest you simply try with "pre_get_posts" hook, like this:
Add below codes into your theme/funcitons.php:

function sbt_exclude_category($query){
         
        if ( is_tax( 'my-taxonomy-slug' ) && $query->is_main_query() && ! is_admin() ) {
             
            $query->set('post_type',array('paret-cpt-slug'));
        }
    }
    add_action('pre_get_posts','sbt_exclude_category');

Please replace "my-taxonomy-slug" with your custom taxonomy slug, replace "paret-cpt-slug" with the parent post type slug.

More help:
https://developer.wordpress.org/reference/hooks/pre_get_posts/