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.

I'm trying to show my CPT Category along with any posts associated with it below the title. I have the first loop fine:

<?php $cats = get_categories(array('taxonomy' => 'custtax', 'orderby' => 'term_group')); 
    if(!empty($cats)) : 
        foreach($cats as $cat) : ?>
            <a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->name; ?></a>
                <br />
            <?php wp_reset_query(); ?>
            <?php $cat_posts = new WP_Query(array('post_type' => 'custcpt', 'taxonomy' => 'custtax', 'terms' => $cat->slug, 'order' => 'ASC'));
            foreach($cat_posts as $cat_post) : ?>

                    <center><?php get_the_post_thumbnail($cat_post->ID, 'thumbnail'); ?></center>
                        <br />
                    <a href="<?php get_permalink($cat_post->ID); ?>"><?php get_the_title($cat_post->ID); ?></a>
                        <br />
                        <br />

            <?php endforeach; ?>
        <?php endforeach; ?>
    <?php endif; ?>

But my second loop queries all my posts instead of just the ones from that category. I've tried get_posts() and instead of pulling all posts it didn't pull any. I'm not sure if 'category' in the codex is expecting a string, slug, or ID, but I've tried all 3. Anyway how do I edit my query to only pull posts from that certain category?

share|improve this question
    
See WP_Query Taxonomy_Parameters for how to correctly format a taxonomy query. –  Milo Oct 17 '13 at 19:10

2 Answers 2

up vote 1 down vote accepted

To expand on the answer from @s_ha_dum - you need to modify a few things about your query.

First, use his suggested tax query:

$args = array(
    'post_type' => 'custcpt', 
    'tax_query' => array(
        array(
            'taxonomy' => 'custtax', 
            'field' => 'id',
            'terms' => $cat->term_id,
        )
    ), 
    'order' => 'ASC'
);

Then, modify your code as follows:

$cat_posts = new WP_Query($args);
while ($cat_posts->have_posts()) :
    $cat_posts->the_post(); 
    // Below, when referencing the post variables, must be as $cat_post->post->ID, etc. ?>
    <center><?php get_the_post_thumbnail($cat_post->post->ID, 'thumbnail'); ?></center>
    <br />
    <?php // Below, don't need to pass the post ID into these functions.  ?>
    <a href="<?php get_permalink(); ?>"><?php get_the_title(); ?></a>
                    <br />
                    <br />

        <?php endwhile; ?>
    <?php endif; ?>

Note a few things:

  1. You must use while ($cat_posts->have_posts) to iterate over the posts.
  2. Call cat_posts->the_post() to prepare the data to be displayed in the functions such as get_the_title()
  3. When referencing post variables, such as ID, you must do so as $cat_posts->post->ID
  4. When calling functions such as get_the_title(), you do not need to pass the ID in.
share|improve this answer

You need a tax_query and what you have is not that.

$cat_posts = new WP_Query(
  array(
    'post_type' => 'custcpt', 
    'tax_query' => array(
      array(
        'taxonomy' => 'custtax', 
        'field' => 'id',
        'terms' => $cat->term_id,
      )
    ), 
    'order' => 'ASC'
  )
);
share|improve this answer
    
Still not pulling any posts, but i'll keep playing around with it –  Howdy_McGee Oct 17 '13 at 19:21
1  
@Howdy_McGee probably because you can't loop over a WP_Query object with foreach, it's an object not an array. –  Milo Oct 17 '13 at 19:30
    
@Milo so is get_posts() a better option then? –  Howdy_McGee Oct 17 '13 at 19:31
    
@Howdy_McGee you can use WP_Query, you just need a loop: while( $cat_posts->have_posts() ): $cat_posts->the_post(), etc.. –  Milo Oct 17 '13 at 19:36
    
@Howdy_McGee : I rarely use anything but WP_Query. –  s_ha_dum Oct 17 '13 at 19:39

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.