0

I've looked around at numerous answers and i can do this for the default 'post' type like so:

 $cats = get_categories(); 
 foreach ($cats as $cat) {
    $cat_id = $cat->term_id;
    echo "<h2>".$cat->name."</h2>";
    query_posts("cat=$cat_id&posts_per_page=100");
    if (have_posts()) : while (have_posts()) : the_post(); ?>
       <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
    <?php endwhile; endif; ?>
 <?php } ?>

Im unable to display posts for a custom post and its associative taxonomy categories. The CPT and its category taxonomy is defined in my functions file. I'm essentially wanting to dispaly all posts related to each category in set of collections. So the logic of the code is pretty much there, I just cant seem to retieve the correct bit of data to filter the posts the way i'd like.

  • 1
    In which template are you doing this. If this is not the main query, the you should rather use WP_Query as you should never use query_posts – Pieter Goosen Dec 9 '14 at 16:10
1

For those who wanted to know:

               <?php $cat_args = array(
                    'taxonomy' => 'service_sections', 
                    'orderby' => 'slug', 
                    'order' => 'ASC'
                );
                $cats = get_categories($cat_args); // passing in above parameters
                foreach ($cats as $cat) : // loop through each cat
                     $cpt_query_args = new WP_Query( array(
                    'post_type' => 'services',
                    'service_sections' => $cat->name
                    )
                );
                if ($cpt_query_args->have_posts()) : ?>
                        <div id="<?php echo $cat->slug; ?>">
                        <?php while ($cpt_query_args->have_posts()) : $cpt_query_args->the_post(); ?>
                            <div class="<?php echo $cat->slug; ?>" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                                <h3 class="staff-name"><?php the_title(); ?></h3>
                                <?php the_content(); ?>
                            </div> 
                        <?php endwhile; ?>
                        </div>
                    <?php endif; 
                    wp_reset_query();
                endforeach; ?>

Updated to eleminate query_posts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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