最新消息: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 - How to add custom post types to normal category pages

matteradmin7PV0评论

I want to have a custom post type, named event, listed with normal posts in the category pages.

I have managed to add categories easily to the custom post type with this:

add_action('init', 'add_category_boxes');
add_action('plugins_loaded','add_category_boxes');
function add_category_boxes()
{
  register_taxonomy_for_object_type('category', 'event');
}

This displays the category checkboxes in wp-admin and works fine. I have then followed the approach here (also suggested elsewhere), as follows:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) 
{
  if(is_category() && $query->is_main_query()) 
  {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('post','event');

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

But the posts of type event still don't come up in the respective category pages. What could be the reason for this? Do I need to add anything else?

The event post types are actually coming from the Events Manager plugin. I don't know if there is something special about these which is stopping them from being displayed in the category pages.

I want to have a custom post type, named event, listed with normal posts in the category pages.

I have managed to add categories easily to the custom post type with this:

add_action('init', 'add_category_boxes');
add_action('plugins_loaded','add_category_boxes');
function add_category_boxes()
{
  register_taxonomy_for_object_type('category', 'event');
}

This displays the category checkboxes in wp-admin and works fine. I have then followed the approach here (also suggested elsewhere), as follows:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) 
{
  if(is_category() && $query->is_main_query()) 
  {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('post','event');

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

But the posts of type event still don't come up in the respective category pages. What could be the reason for this? Do I need to add anything else?

The event post types are actually coming from the Events Manager plugin. I don't know if there is something special about these which is stopping them from being displayed in the category pages.

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Dec 19, 2014 at 12:45 jbxjbx 2813 silver badges16 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 1

You have one or two problems here

  • is_category() should be object of $query in your example code

  • get_query_var('post_type') will always return false on a category page AFAIK, so that code is totally unnecessary

  • Just a tip, when using pre_get_posts with any type of archive, also check for non admin pages as your back end will also be affected by this change

You can try something like this

add_action( 'pre_get_posts', function ( $q ) {
    if( !is_admin() && $q->is_main_query() && $q->is_category() ) {
        $q->set( 'post_type', array( 'post','event' ) );
    }
});

I had similar problem and I solved by passing the nav_menu_item to the $post_type for the menu to appear properly. In your case would be:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) 
{
  if(is_category() && $query->is_main_query()) 
  {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = array( 'nav_menu_item', $post_type);
    else
        $post_type = array( 'nav_menu_item', 'post', 'event' );

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

For creating taxonomy like category & tags for custom post type use register_taxonomy wordpress function

Refer this wordpress codex for register_taxonomy

This seems to solve the issue:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type( $query ) {
  if( is_category() &&  $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'nav_menu_item', 'event'));
      return $query;
    }
}
Post a comment

comment list (0)

  1. No comments so far