2

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
    Before I start to investigate the issue, can you please try and use dash (-) instead of underline (_) in your taxonomy's name? I'm not sure if this matters, but I've seen a couple of problems with such cases. – Jack Johansson Jul 23 '17 at 0:45
  • Still the same unfortunately - I'm trying to see if it has anything to do with using have_posts() – j.grima Jul 23 '17 at 5:12
  • Figured it out, exclude_from_search was set to true on register_post_type – j.grima Jul 23 '17 at 5:41
2

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.

  • 1
    I was quite skeptical that this was what prevented my private CPTs from displaying on my custom taxonomy archive, but it was indeed the culprit. exclude_from_search => false ftw! – cfx Feb 8 at 21:08

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.