Custom Taxonomy returns no posts

I have created a custom post type called video and a custom taxonomy for the custom post’s categories video_category. taxonomy-video_category.php is the file that is handling the taxonomy however, it’s never returning any posts even though posts of type video_category exist.

This is the code used to create the taxonomy, can’t seem to find what’s exactly wrong so far:

function taxonomies_video() {

        $labels = array(
            'name' => _x('Videos Categories', 'taxonomy general name'),
            'singular_name' => _x('Video Category', 'taxonomy singular name'),
            'search_items' => __('Search Video Categories'),
            'all_items' => __('All Video Categories'),
            'parent_item' => __('Parent Video Category'),
            'parent_item_colon' => __('Parent Video Category:'),
            'edit_item' => __('Edit Video Category'),
            'update_item' => __('Update Video Category'),
            'add_new_item' => __('Add New Video Category'),
            'new_item_name' => __('New Video Category'),
            'menu_name' => __('Video Categories')
        );

        $args = array(
            'labels' => $labels,
            'hierarchical' => true
        );

        register_taxonomy('video_category', 'video', $args);
    }
    add_action('init', 'taxonomies_video', 0);

Code used in taxonomy-video_category.php:

// ...

    <?php if ( have_posts() ) : ?>

        <div class="entry-content">

            <?php while ( have_posts() ) : the_post(); ?>

                <?php
                    get_template_part( 'template-parts/content', 'video_cat' );
                ?>

            <?php endwhile; ?>

        </div>

    <?php else : ?>

        <?php get_template_part( 'template-parts/content', 'none' ); ?>

    <?php endif; ?>

// ...

1 Answer
1

Finally I’ve figured it out, I didn’t elaborate on this in my question but I had exclude_from_search set to true on register_post_type which is obviously why no posts were being displayed in the taxonomy-video_category.php template file.

Changed it to false and posts are now displayed.

// ...

$args = array(
    'labels' => $labels,
    'description' => 'Holds our Videos and Video specific data',
    'public' => true,
    'exclude_from_search' => false,
    'menu_position' => 5,
    'supports' => array('title', 'editor', 'page-attributes', 'thumbnail'),
    'has_archive' => true,
    'rewrite' => array('slug' => 'how-to-videos')
);

register_post_type('video', $args);

// ...

Further explanation on exclude_from_search in this question.

Leave a Comment