1

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.

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' ) );
    }
});
1

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;
  }
}
0

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

Refer this wordpress codex for register_taxonomy

  • The problem is not related to register_taxonomy. As I mentioned above, the checkboxes for the categories already display fine in the admin screen when editing the custom post type. – jbx Dec 22 '14 at 14:21
0

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;
    }
}
  • And how does this differ from my answer – Pieter Goosen Dec 22 '14 at 14:24
  • @PieterGoosen The problem was that in both my original method and in yours we are both using is_main_query(). For some reason when I removed that, the custom posts appeared as I wanted. The nav_menu_item is required however when the is_main_query() is removed, because otherwise menus stop working. – jbx Dec 22 '14 at 15:04
  • You know this will effect all oother custom queries, even widgets if you remove is_main_query(). How does the page look that you need to modify. If it is a custom query, then you should not be using pre_get_posts – Pieter Goosen Dec 22 '14 at 15:06
  • From what you are saying, there is another bigger issue somewhere – Pieter Goosen Dec 22 '14 at 15:06
  • It seems to be working fine so far. What should I look out for? – jbx Dec 22 '14 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.