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.
4 Answers
Reset to default 1You have one or two problems here
is_category()
should be object of$query
in your example codeget_query_var('post_type')
will always return false on a category page AFAIK, so that code is totally unnecessaryJust 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;
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745599128a4635294.html
评论列表(0条)