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

taxonomy - Custom post types not showing on the standard Wordpress Category page

programmeradmin1浏览0评论

I’ve taken over website from another developer and they used a plugin to generate a custom post types. The plugin is no longer maintained and sometimes courses an error 505 when try and load the plugins admin page. The front end site works fine. I also don't like having out of date plugins because of the security risk. ' I previously asked a question related to this issue which was solved about getting the properties of a custom post type created by a plugin so they I could re make it using register_post_type(). That was solved by Qaisar Feroz with get_post_type_object().

I have now recreated the post types and everything works fine in the back end. I can see all my posts that are custom post types. The only remaining problem is that one of the post types uses the standard Wordpress category as its taxonomy. Custom post types are not showing up on the category pages like they used to, if I was making the site from scratch I would use a custom taxonomy but all the posts and their links to the categories already exist. I have read that if a custom post type uses Wordpress' standard taxonomy's: tags & category you have to take extra steps for the custom post types to show up on the category archive pages. I tried several code snippets but every-time I use something like the one below my custom post types will display on the category page but none of my menus will display in the header or the footer. Its must be possible as it all worked fine with the plugin (Toolset Types)

My custom post type is called 'product'.

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if( is_category() ) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('nav_menu_item', 'post', 'product'); // don't forget nav_menu_item to allow menus to work!
        $query->set('post_type',$post_type);
        return $query;
    }
}

I’ve taken over website from another developer and they used a plugin to generate a custom post types. The plugin is no longer maintained and sometimes courses an error 505 when try and load the plugins admin page. The front end site works fine. I also don't like having out of date plugins because of the security risk. ' I previously asked a question related to this issue which was solved about getting the properties of a custom post type created by a plugin so they I could re make it using register_post_type(). That was solved by Qaisar Feroz with get_post_type_object().

I have now recreated the post types and everything works fine in the back end. I can see all my posts that are custom post types. The only remaining problem is that one of the post types uses the standard Wordpress category as its taxonomy. Custom post types are not showing up on the category pages like they used to, if I was making the site from scratch I would use a custom taxonomy but all the posts and their links to the categories already exist. I have read that if a custom post type uses Wordpress' standard taxonomy's: tags & category you have to take extra steps for the custom post types to show up on the category archive pages. I tried several code snippets but every-time I use something like the one below my custom post types will display on the category page but none of my menus will display in the header or the footer. Its must be possible as it all worked fine with the plugin (Toolset Types)

My custom post type is called 'product'.

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if( is_category() ) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('nav_menu_item', 'post', 'product'); // don't forget nav_menu_item to allow menus to work!
        $query->set('post_type',$post_type);
        return $query;
    }
}
Share Improve this question asked May 10, 2019 at 14:33 lomokevlomokev 1831 gold badge2 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You don't need to return $query it is passed by reference.Have a look at the codex

The return statement might be shortcircuiting the page load. Try removing it.

发布评论

评论列表(0)

  1. 暂无评论