0

I have created a template for my custom taxonomy, byron_community_cat. I have named it taxonomy-byron_community_cat.php. My problem is, when using the default WordPress loop, it does not display any posts. I have created some posts and have added them to a term in this taxonomy. Below is my code for the post type and taxonomy.

/**
 * Registers the Community post type
 * @return void
 */
function byron_community_register()
{
    register_post_type('byron_community', [
        'labels' => [
            'name' => __('Community posts', 'byroncommunity'),
            'singular_name' => __('Community post', 'byroncommunity'),
        ],
        'public' => true,
        'exclude_from_search' => true,
        'show_in_nav_menus' => false,
        'menu_position' => 40,
        'taxonomies' => ['byron_community_cat'],
        'menu_icon' => 'dashicons-groups',
        'supports' => ['title', 'editor', 'thumbnail', 'excerpt'],
        'has_archive' => __('community', 'byroncommunity'),
        'rewrite' => [
            'slug' => __('community', 'byroncommunity'),
            'with_front' => false,
        ],
    ]);
}
add_action('init', 'byron_community_register');

/**
 * Registers the Community category taxonomy for the Community post type
 * @return void
 */
function byron_community_register_category()
{
    register_taxonomy('byron_community_cat', 'byron_community', [
        'public' => true,
        'hierarchical' => true,
        'show_admin_column' => true,
        'rewrite' => [
            'slug' => 'community_category',
            'with_front' => false
        ]
    ]);
    register_taxonomy_for_object_type('byron_community_cat', 'byron_community');
}
add_action('init', 'byron_community_register_category');

I use the default loop like this in my taxonomy template, yet, it does not display any posts:

if (have_posts()) {
    while (have_posts()) {
        the_post();
        //HTML stuff...
    }
}

The right template is being chosen, because changes outside the loop, are reflected on the page. I don't know what I'm doing wrong, I suspect it might have something to do with the way I've registered the taxonomy. Any help would be greatly appreciated.

  • Did you flush your permalink setting? Btw you don't need to call this register_taxonomy_for_object_type('byron_community_cat', 'byron_community');, you already did this in CPT and taxonomy registration. – obiPlabon Feb 20 '18 at 15:27
  • I did flush the permalinks. Thanks for the tip, I'll remove the line. I added it to be sure that wasn't the problem. – Connor Feb 20 '18 at 15:38
2

I think the issue here is that when registering the post type, you've set exclude_from_search to true.

See this note from the WordPress Coedex:

Note: If you want to show the posts's list that are associated to taxonomy's terms, you must set exclude_from_search to false (ie : for call site_domaine/?taxonomy_slug=term_slug or site_domaine/taxonomy_slug/term_slug). If you set to true, on the taxonomy page (ex: taxonomy.php) WordPress will not find your posts and/or pagination will make 404 error...

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.