Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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; ?>
share|improve this answer
    
Thanks for the reply! I ended up finally figuring it out. –  ftntravis Jul 17 '12 at 4:00
    
@ftntravis I took the liberty to edit your question, take the answer and add it in mine. (Answers are not supposed to be in questions.) If you want I can edit my answer and remove it, so that you can add your own answer. :) –  its_me Jul 17 '12 at 11:15
    
That's fine. Thanks! –  ftntravis Jul 21 '12 at 23:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.