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

custom post types - Category permalinks don't work even after flushing

programmeradmin4浏览0评论

I have created a custom post type (portfolio) and I'm using the built-in Category taxonomy with it. When I view the category archives page, only regular posts are showing under the category, my custom posts aren't there at all. The options to select categories are there in the Edit Post page (and Quick Edit), they just aren't showing up on the front end.

I have tried flushing the permalinks by saving under Settings > Permalinks, and I have also tried including flush_rewrite_rules(); in my functions.php. Nothing works?

Here is my custom post type code (note: my custom posts work fine with rewrite rules, it's just the categories):

/**
 * Registers the 'apc_portfolio' custom post type.
 *
 * @example register_post_type( $taxonomy, $args );
 * @see 
 * @todo change public, to false
 */
function apc_register_portfolio_post_type() {

    $labels  = array(
        'add_new_item'          => __( 'Add New Portfolio Item', 'detail-factory-2020' ),
        'add_new'               => __( 'Add New Portfolio', 'detail-factory-2020' ),
        'all_items'             => __( 'All Portfolio Items', 'detail-factory-2020' ),
        'archives'              => __( 'Portfolio Item Archives', 'detail-factory-2020' ),
        'attributes'            => __( 'Portfolio Item Attributes', 'detail-factory-2020' ),
        'edit_item'             => __( 'Edit Portfolio Item', 'detail-factory-2020' ),
        'featured_image'        => __( 'Featured Image', 'detail-factory-2020' ),
        'filter_items_list'     => __( 'Filter Portfolio Items list', 'detail-factory-2020' ),
        'insert_into_item'      => __( 'Insert into Portfolio Item', 'detail-factory-2020' ),
        'items_list_navigation' => __( 'Portfolio Items list navigation', 'detail-factory-2020' ),
        'items_list'            => __( 'Portfolio Items list', 'detail-factory-2020' ),
        'menu_name'             => __( 'Portfolio', 'detail-factory-2020' ),
        'name_admin_bar'        => __( 'Portfolio Item', 'detail-factory-2020' ),
        'name'                  => _x( 'Portfolio Items', 'Post Type General Name', 'detail-factory-2020' ),
        'new_item'              => __( 'New Portfolio Item', 'detail-factory-2020' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'detail-factory-2020' ),
        'not_found'             => __( 'Not found', 'detail-factory-2020' ),
        'parent_item_colon'     => __( 'Parent Portfolio Item:', 'detail-factory-2020' ),
        'remove_featured_image' => __( 'Remove featured image', 'detail-factory-2020' ),
        'search_items'          => __( 'Search Portfolio Item', 'detail-factory-2020' ),
        'set_featured_image'    => __( 'Set featured image', 'detail-factory-2020' ),
        'singular_name'         => _x( 'Portfolio Item', 'Post Type Singular Name', 'detail-factory-2020' ),
        'update_item'           => __( 'Update Portfolio Item', 'detail-factory-2020' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Portfolio Item', 'detail-factory-2020' ),
        'use_featured_image'    => __( 'Use as featured image', 'detail-factory-2020' ),
        'view_item'             => __( 'View Portfolio Item', 'detail-factory-2020' ),
        'view_items'            => __( 'View Portfolio Items', 'detail-factory-2020' ),
    );
    $rewrite = array(
        'feeds'      => false,
        'pages'      => false,
        'slug'       => 'work',
        'with_front' => true,
    );
    $args    = array(
        'can_export'          => true,
        'capability_type'     => 'post',
        'exclude_from_search' => false,
        'has_archive'         => true,
        'hierarchical'        => false,
        'label'               => __( 'Portfolio Item', 'detail-factory-2020' ),
        'labels'              => $labels,
        'menu_icon'           => 'dashicons-portfolio',
        'menu_position'       => 5,
        'public'              => true,
        'publicly_queryable'  => true,
        'rewrite'             => $rewrite,
        'show_in_admin_bar'   => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_rest'        => true,
        'show_ui'             => true,
        'supports'            => array(
            'custom-fields',
            'editor',
            'excerpt',
            'post-formats',
            'thumbnail',
            'title',
        ),
        'taxonomies'          => array( 'category' ),
    );
    register_post_type( 'apc-portfolio', $args );

}
add_action( 'init', 'apc_register_portfolio_post_type', 0 );

I have created a custom post type (portfolio) and I'm using the built-in Category taxonomy with it. When I view the category archives page, only regular posts are showing under the category, my custom posts aren't there at all. The options to select categories are there in the Edit Post page (and Quick Edit), they just aren't showing up on the front end.

I have tried flushing the permalinks by saving under Settings > Permalinks, and I have also tried including flush_rewrite_rules(); in my functions.php. Nothing works?

Here is my custom post type code (note: my custom posts work fine with rewrite rules, it's just the categories):

/**
 * Registers the 'apc_portfolio' custom post type.
 *
 * @example register_post_type( $taxonomy, $args );
 * @see https://codex.wordpress/register_post_type#Parameters
 * @todo change public, to false
 */
function apc_register_portfolio_post_type() {

    $labels  = array(
        'add_new_item'          => __( 'Add New Portfolio Item', 'detail-factory-2020' ),
        'add_new'               => __( 'Add New Portfolio', 'detail-factory-2020' ),
        'all_items'             => __( 'All Portfolio Items', 'detail-factory-2020' ),
        'archives'              => __( 'Portfolio Item Archives', 'detail-factory-2020' ),
        'attributes'            => __( 'Portfolio Item Attributes', 'detail-factory-2020' ),
        'edit_item'             => __( 'Edit Portfolio Item', 'detail-factory-2020' ),
        'featured_image'        => __( 'Featured Image', 'detail-factory-2020' ),
        'filter_items_list'     => __( 'Filter Portfolio Items list', 'detail-factory-2020' ),
        'insert_into_item'      => __( 'Insert into Portfolio Item', 'detail-factory-2020' ),
        'items_list_navigation' => __( 'Portfolio Items list navigation', 'detail-factory-2020' ),
        'items_list'            => __( 'Portfolio Items list', 'detail-factory-2020' ),
        'menu_name'             => __( 'Portfolio', 'detail-factory-2020' ),
        'name_admin_bar'        => __( 'Portfolio Item', 'detail-factory-2020' ),
        'name'                  => _x( 'Portfolio Items', 'Post Type General Name', 'detail-factory-2020' ),
        'new_item'              => __( 'New Portfolio Item', 'detail-factory-2020' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'detail-factory-2020' ),
        'not_found'             => __( 'Not found', 'detail-factory-2020' ),
        'parent_item_colon'     => __( 'Parent Portfolio Item:', 'detail-factory-2020' ),
        'remove_featured_image' => __( 'Remove featured image', 'detail-factory-2020' ),
        'search_items'          => __( 'Search Portfolio Item', 'detail-factory-2020' ),
        'set_featured_image'    => __( 'Set featured image', 'detail-factory-2020' ),
        'singular_name'         => _x( 'Portfolio Item', 'Post Type Singular Name', 'detail-factory-2020' ),
        'update_item'           => __( 'Update Portfolio Item', 'detail-factory-2020' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Portfolio Item', 'detail-factory-2020' ),
        'use_featured_image'    => __( 'Use as featured image', 'detail-factory-2020' ),
        'view_item'             => __( 'View Portfolio Item', 'detail-factory-2020' ),
        'view_items'            => __( 'View Portfolio Items', 'detail-factory-2020' ),
    );
    $rewrite = array(
        'feeds'      => false,
        'pages'      => false,
        'slug'       => 'work',
        'with_front' => true,
    );
    $args    = array(
        'can_export'          => true,
        'capability_type'     => 'post',
        'exclude_from_search' => false,
        'has_archive'         => true,
        'hierarchical'        => false,
        'label'               => __( 'Portfolio Item', 'detail-factory-2020' ),
        'labels'              => $labels,
        'menu_icon'           => 'dashicons-portfolio',
        'menu_position'       => 5,
        'public'              => true,
        'publicly_queryable'  => true,
        'rewrite'             => $rewrite,
        'show_in_admin_bar'   => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_rest'        => true,
        'show_ui'             => true,
        'supports'            => array(
            'custom-fields',
            'editor',
            'excerpt',
            'post-formats',
            'thumbnail',
            'title',
        ),
        'taxonomies'          => array( 'category' ),
    );
    register_post_type( 'apc-portfolio', $args );

}
add_action( 'init', 'apc_register_portfolio_post_type', 0 );
Share Improve this question asked Jul 10, 2020 at 8:26 gillespiezagillespieza 1,2855 gold badges26 silver badges46 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The default WP category is only set-up to show posts out of the box. You should be able to expand this by doing something like the below in your functions.php:

// Order course archive and taxonomy views
function adjust_main_query($query){
    if ( ! is_admin() && is_category() && is_category() && $query->is_main_query() ) {

        $query->set( 'post_type', array( 'post', 'mpCPT' ));
    }
}
add_action( 'pre_get_posts', 'adjust_main_query' );

I haven't tested this, but it's an adaptation of a working example I have used. Hope it helps get you on the right lines.

发布评论

评论列表(0)

  1. 暂无评论