1

I have created a number of custom post types, but when I visit the "tag" page which should be listing those custom post types, it has no posts to show.

I created my custom post types like so:

$news_attr = array(
    'name' => 'News',
    'singluar_name' => 'News Article',
    'add_new' => 'Add News Article',
    'all_items' => 'All News Articles',
    'add_new_item' => 'Add News Article',
    'edit_item' => 'Edit News Article',
    'new_item' => 'New News Article',
    'view_item' => 'View News Article',
    'search_item' => 'Search News Articles',
    'not_found' => 'No Podcast found',
    'not_found_trash' => 'No Podcast found in Trash',
    'parent_item_colon' => 'Parent Item'
);

$news_args = array(
    'labels' => $news_attr,
    'public' => true,
    'has_archive' => true,
    'publicly_queryable' => true,
    'query_var' => true,
    'capability_type' => 'post',
    'heirarchical' => 'false',
    'supports' => array(
        'thumbnail',
        'post-thumbnails',
        'title',
        'editor',
        'excerpt',
        'revisions',
        'custom-fields',
        'author'
    ),
    'taxonomies' => array('category', 'post_tag'),
    'menu_position' => 4,
    'exclude_from_search' => false,
    'rewrite' => array('slug' => 'news-archive'),
    'menu_icon'   => 'dashicons-welcome-widgets-menus'
);

If I visit /tag/blog-articles/, it lists all the standard post-types, but none of the custom ones. How would I ensure that custom post-types are included in this?

1 Answer 1

1

WordPress, by default, shows only posts on tags and categories archives... So if you want to show your custom post type in there, then you'll have to make WordPress do that...

function add_news_articles_to_categories_and_tags( $query ) {
    if ( ! is_admin() && $query->is_main_query() && ($query->is_category || $query->is_tax ) ) {
        $query->set( 'post_type', array('post', 'news_article') ); // use your CPT slugs in here
    }
}
add_action( 'pre_get_posts', 'add_news_articles_to_categories_and_tags' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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