List posts AND custom post type by category

I found this question asked elsewhere, but it was unanswered, so I’m asking myself.

I have a custom post type (Features) that shares my Categories and Tags with my standard posts. I’m listing my categories with this:

Read More
<?php wp_list_categories('title_li=&hierarchical=false'); ?>

The resulting list includes categories that are applied only to Features. I know this is working because some of these point to empty pages. So the problem is with the loop that I’m using on my archive page.

Here’s the loop:

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

    <?php $custom_classes = get_post_meta($post->ID, 'custom_post_class', false);  ?>
    <article <?php post_class($custom_classes) ?> id="post-<?php the_ID(); ?>" role="article">
        <header>
            <hgroup>
                <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
                <h2 class="vcard">By <?php the_author_posts_link() ?></h2>
                <h3><time datetime="<?php echo get_the_date('c'); ?>"><?php echo get_the_date('F jS, Y'); ?></time></h3>
            </hgroup>
        </header>
        <section class="text clearfix">
            <?php the_content(); ?>
            <?php edit_post_link('Edit this post ✍', '<p>', '</p>'); ?>
        </section>
    </article>

<?php endwhile; ?>

Can you tell me what I’d need to add in order to get my custom post type to show up here?

Related posts

Leave a Reply

2 comments

  1. I found this answer on the WordPress forum to be of help:

    http://wordpress.org/support/topic/custom-post-type-posts-not-displayed?replies=5#post-2805344

    basically, the poster references a post by Justin Tadlock (who is pretty much an authority in WordPress theme and plugin desing by the way. Follow his blog if you want to get savvy on WordPress.)

    http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page

    In his blog post he provides a code snippet that you can add to your functions.php page:

    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    function my_get_posts( $query ) {
    
        if ( is_home() && $query->is_main_query() )
            $query->set( 'post_type', array( 'post', 'page', 'album', 'movie', 'quote' ) );
    
        return $query;
    }
    

    and this seems to work fairly well. Of course, you might want to change is_home() to is_archive() and substitute album, movie, quote for your custom post type handles as you’ve registered them with the register_post_type function.

  2. First, check if this is set in your code for registering the CPT: 'has_archive' => true.

    Next, do you have an archive page for the custom post? Although it is not mandatory that you should have an archive page for the CPT, as the main index will be used as a fallback, I think making a CPT archive file should solve your problem. Copy this code to a new php file and name it as archive-feature.php (where ‘feature’ is your custom post type, please check if the name and case is right). Also you might need a singular template for your CPT single post. That file should be named as single-feature.php. For more on template files: Template Hieracrhy for CPTs. Hope this helps!