$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - multiple custom post type on category page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - multiple custom post type on category page

matteradmin10PV0评论

Is it possible to have multiple custom post types appear on a category page?

For example, I have a category Animals and I can assign it to both these custom post types: dog, cat

Both cpts use a pre_get_posts filter:

function dog_query_post_type($query) {

    if( is_category() &&  $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
        $post_type = get_query_var('post_type');
        if($post_type)
            $post_type = $post_type;
        else
            $post_type = array( 'post', 'dog', 'nav_menu_item');

        $query->set('post_type',$post_type);

        return $query;
    }

}
add_filter('pre_get_posts', 'dog_query_post_type');

But only one cpt will appear on the Animals category page.

If I deactivate the plugin for the dogs cpt, then any cat cpts assigned to Animals will appear on the category page.

I have tried changing the priority settings on the add_filter calls.

And none of this affects the display of normal posts in the category from appearing on the category page.

I'd like to have any post, of any type, appear on the Animals page.

What am I missing?

Is it possible to have multiple custom post types appear on a category page?

For example, I have a category Animals and I can assign it to both these custom post types: dog, cat

Both cpts use a pre_get_posts filter:

function dog_query_post_type($query) {

    if( is_category() &&  $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
        $post_type = get_query_var('post_type');
        if($post_type)
            $post_type = $post_type;
        else
            $post_type = array( 'post', 'dog', 'nav_menu_item');

        $query->set('post_type',$post_type);

        return $query;
    }

}
add_filter('pre_get_posts', 'dog_query_post_type');

But only one cpt will appear on the Animals category page.

If I deactivate the plugin for the dogs cpt, then any cat cpts assigned to Animals will appear on the category page.

I have tried changing the priority settings on the add_filter calls.

And none of this affects the display of normal posts in the category from appearing on the category page.

I'd like to have any post, of any type, appear on the Animals page.

What am I missing?

Share Improve this question asked Dec 5, 2018 at 16:53 shanebpshanebp 5,0857 gold badges28 silver badges40 bronze badges 2
  • Whatever filter runs last is going to overwrite whatever ran before it, as you're not adding to post types already set by previous filters, you are replacing it entirely with a different value. – Milo Commented Dec 5, 2018 at 17:07
  • So obvious I couldn't see it. Thx. – shanebp Commented Dec 5, 2018 at 18:34
Add a comment  | 

1 Answer 1

Reset to default 2

Add to $post_type array, don't replace it...

function dog_query_post_type($query) {

    if( is_category() &&  $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
        $post_type = get_query_var('post_type');
        if($post_type)
            $post_type[] = 'dog';
        else
            $post_type = array( 'post', 'dog', 'nav_menu_item');

        $query->set('post_type',$post_type);

        return $query;
    }

}
add_filter('pre_get_posts', 'dog_query_post_type');
Post a comment

comment list (0)

  1. No comments so far