WordPress.org

Ready to get started?Download WordPress

Forums

Get all posts from categories (2 posts)

  1. Blindacme
    Member
    Posted 1 year ago #

    I am trying to create a page template that will display all posts (custom post types) from all categories (custom taxonomies). I found a similar thread on here that got me started and I am almost there. What is happening is that for each category it shows all posts from all categories. In other words I would like it to be similar to this:

    Category 1:
    Category 1's Post #1
    Category 1's Post #2
    Category 1's Post #3
    etc.

    Category 2:
    Category 2's Post #1
    Category 2's Post #2
    Category 2's Post #3
    etc.

    and so on.

    The problem with the below code is that it is pulling all posts and filing them under each category where I need it to be all posts for that specific category - for all categories. I feel I am close but not quite there.

    Here is what I am working with:

    <?php
    //for each category, show all posts
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'taxonomy' => 'closeouts_categories'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => -1,
          'category_in' => array($category->term_id),
          'caller_get_posts'=>1,
    	  'post_type' => 'our_closeouts',
    	  'taxonomy' => 'closeouts_categories'
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

    Any takers? Thanks in advance for the help

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I think you will get what you want if you change this:

    'category_in' => array($category->term_id),

    to this:

    'category__in' => array($category->term_taxonomy_id),

    Note the double underscores in 'category__in'. Also, a given term might belong to more than one taxonomy, so you need to use term_taxonomy_id instead of just term_id.

Topic Closed

This topic has been closed to new replies.

About this Topic