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:

<?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="https://wordpress.stackexchange.com/questions/33864/<?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?

2 Answers
2

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.

Leave a Comment