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 need to display all the categories I created for a custom post type then inside of each category I need to loop all the posts that are tied to that category.

I tried constructing my WP_Query a number of different ways and I can't get it to work at all.

Here's the code I have now:

$categories = get_categories('taxonomy=faqcat&order=DESC');
foreach ($categories as $cat) {

    // loop through all  posts tied to category here
}

Updated Code.. still doesn't work.. keeps displaying the same posts in each category.

<?php
$categories = get_categories('taxonomy=faqcat&order=DESC');
foreach ($categories as $cat) :

    echo '<h1>' . $cat->name . ' (' . $cat->cat_ID . ' )</h1>';
    $q = new WP_Query(array('cat_ID' => $cat->cat_ID, 'post_type' => 'faq', 'tax_query' => array('taxonomy' => 'faqcat')));
    if ($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
      echo $post->ID;      

    ?>
            <pre> <B><?php the_title(); ?></b></pre>
            <p><?php the_content(); ?></p>
            <br/>
        <?php endwhile;
    else:
        ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php
    endif;
endforeach;
?>
share|improve this question
2  
This is a recursively confusing question. –  Brian Fegter Jul 24 '12 at 1:40
2  
Please do not hide your code on a third party site. It is and should be part of your question, so later readers can understand what the problem was and if it can help them with their problem. –  toscho Jul 24 '12 at 2:27

1 Answer 1

up vote 1 down vote accepted

Here's how I fixed it..

    <!--faq page -->
    <div class="faq">
        <?php get_template_part('./template/global/breadcrumbs'); ?>
        <h3><span><?php the_title(); ?></span></h3>
        <?php
        $categories = get_categories('taxonomy=faqcat&order=DESC');
        foreach ($categories as $cat) {
            $i = 0;
            ?>
            <div class="faq-title">
                <h2><?php echo $cat->name; ?></h2>
            </div>
            <?php
            $answers = new WP_Query(array('post_type' => 'faq', 'tax_query' => array(array('taxonomy' => 'faqcat', 'field' => 'id', 'terms' => $cat->term_id,),),));
            if ($answers->have_posts()) : while ($answers->have_posts()) : $answers->the_post();
                    $i++;
                    ?>
                    <div class="faq-post" id="faq-<?php echo $i; ?>"> <span class="close-tab"><?php the_title(); ?></span>
                        <div class="faq-post-detail">
                            <?php the_content(); ?>  
                        </div>
                    </div>

                    <?php
                endwhile;
            else:
                ?>
                <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
            <?php endif;
        }
        ?>





    </div>

</div>
<!--/content -->
share|improve this answer

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.