views:

94

answers:

2

I want to have a page that shows all posts, separated by category. The idea is to get the categories, and then iterate through all posts for each category. The problem is complicated by the fact that I want to iterate through all posts of a given custom type, using a custom taxonomy as the categories. (Running Wordpress 3)

In my functions.php, my custom post type is registered as "video" and the custom taxonomy as "video_types".

In my custom page template that is supposed to show all videos arranged by category, this is the code that isn't returning any posts (and they're there, I checked):

<?php 
  $categories = get_categories(array(
    'taxonomy' => 'video_types'
  )); 
  foreach ($categories as $cat):
?>
 <section id="<?php $cat->slug ?>" class="video-category">
     <?php
  query_posts(array(
      'cat' => $cat->cat_ID,
      'posts_per_page' => -1
         ));
     ?>
     <h2><?php single_cat_title(); ?></h2>
    <p class="description"><?php echo category_description($cat->cat_ID); ?></p>
  <?php while (have_posts()) : the_post(); ?>
      <?php
       $category = get_the_category(); 
            echo $category[0]->cat_name;
      ?>
      <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <article class="video">
        <h3><?php the_title(); ?></h3>
        <p>
          <?php the_content() ?>
        </p>
      </article>
  <?php endwhile; ?>
 </section>
<?php endforeach; ?>
A: 

You might try another approach. Try using get_posts to get your posts sorted by your custom taxonomy, set up a variable that is initially an empty string (called $current_cat or something), and with each loop of the results, check the taxonomy and compare it with $current_cat - if different, print out a header for the new category and then the entry, if the same, print out an entry as usual.

A clear issue with your code (I believe) is that you're not properly querying your custom taxonomy. You should be using simply taxonomy_name => 'value' within your query, a custom taxonomy won't be touched by cat in a query.

Let me know if you need more detail.

edit: More detail!

// get a list of categories, in this case your custom taxonomy (your_taxonomy_name)
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'your_taxonomy_name'";

$categories = $wpdb->get_results($querystr, OBJECT);

foreach( $categories as $category ): // begin a loop through those terms (categories in your custom taxonomy)
    echo '<div class="category-header"><h3>'.$category->name.'</h3>'; // print the cat title
    echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'your_taxonomy_name')).'</p></div>'; // cat description

    $posts = get_posts( array( 'your_taxonomy_name' => $category->name, 'post_type' => 'your_post_type' ) ) //select posts in this category, and of a specified content type
    foreach($posts as $post) : // begin cycle through posts of this category
        setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)

        [ ... ] // do things with your post (display it)

    endforeach;

endforeach;

That should do it - and this may be helpful for using get_posts.

Gavin
Hey man, thanks for the answer. Can you go explain your method a little bit more? Specifically the first part about using get posts.
Diogenes
Oh, and I should note that the reason I wanted to get the categories first was because I need their description for the page, the idea being to have a category name, description, and then all the videos (post_type => 'video') in that category (or "video_type") for every video type.
Diogenes
Totally doable - editing the answer above with some code.
Gavin