最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How to add custom post types to normal category pages

programmeradmin1浏览0评论

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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论