Display related custom taxonomy posts in sidebar

Can I use this snippet to retrieve posts from a custom taxonomy in a custom post type?

<?php $sermon_series = new WP_Query( array('series' => 'type')); ?>

<?php while ($sermon_series->have_posts()) : $sermon_series->the_post(); ?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endwhile; ?>

It displays regular posts now, but I need it to display posts from a custom post type called “Sermons” in my wordpress.

Related posts

Leave a Reply

1 comment

  1. Reference: WordPress Codex — Custom Taxonomies > Querying by taxonomy

    Creating a taxonomy generally automatically creates a special query variable using WP_Query class, which we can use to retrieve posts based on. For example, to pull a list of posts that have “Bob” as a “person” taxonomy in them, we will use:

    $query = new WP_Query( array( 'person' => 'bob' ) );
    

    or, for more complex argument:

    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'person',
                'field' => 'slug',
                'terms' => 'bob'
            )
        )
    );
    $query = new WP_Query( $args );
    

    What’s above is quoted from the document I linked far above, and I think it’s very very relevant/close to what you are doing.

    Solution:

    <?php query_posts(array( 'post_type' => 'sermon', 'sermon_series' => $sermon_series )); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <div class="post">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <small><?php the_time('F jS, Y') ?></small>
    </div>
    
    <?php endwhile; ?>
    <?php endif; ?>