0
votes

I have created a custom post type in Wordpress and created a taxonomy for it. Now I want to display the taxonomy-terms and under each term display the posts that have that term. I'm trying to use get_posts for that, but get-posts just turns up empty. Here is my code with comments on what happens:

<?php
//for each category, show all posts
$cat_args=array(
  'orderby' => 'name',
  'order' => 'ASC',
  'taxonomy' => 'teachres-categorie',
  'post_type'=> 'biology'
);

$categories=get_categories($cat_args);

foreach($categories as $category) {
$args=array(
   'showposts' => -1,
   'category' => array($category->term_taxonomy_id),
   'ignore_sticky_posts'=>1,
   'post_type'=> 'biology', 
   'taxonomy' => 'teachres-categorie'
 );
 ?> 

 <h2><?php  echo '<p>Category: <a href="' . get_category_link($category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';?></h2>
 <p><?php echo $category->term_taxonomy_id; ?></p> 

 //this gets displayed just fine, meaning $category-array is filled

 <?php
 $posts = get_posts($args);

 //at this point $args is filled: Array ( [showposts] => -1 [category] => Array ( [0] => 34 ) [ignore_sticky_posts] => 1 [post_type] => biology [taxonomy] => teachres-categorie ) 
 //at this point $posts is empty: Array ( ) 

   if ($posts) {

    //This never gets executed as $posts is empty

     foreach($posts as $post) {
    // ...so we'll never get to here            

     } // end foreach($posts
   } // end if ($posts
 } // // end foreach($categories
?>

Other than this code I didn't alter the main post-query and these are the only loops on the page. When I search on this, there are more people with the same problem, but none of their solutions are helpful in my case.

Can anyone tell me why get_posts is empty?

2

2 Answers

1
votes

There is few errors in your get_posts arguments. Considering you actually have the following based on your comments:

$posts = get_posts(array(
   'showposts' => -1,
   'category' => array(0 => 34),
   'ignore_sticky_posts' => 1,
   'post_type'=> 'biology', 
   'taxonomy' => 'teachres-categorie'
));
  • The category parameter expect an integer (id of the category) or string containing comma separated list of the category IDs. From the doc:

    The category parameter can be a comma separated list of categories, as the get_posts() function passes the 'category' parameter directly into WP_Query as 'cat'.

    Then if you look into the WP_Query documentation you will see that the array values for categories are expected only for the category__and, category__in and category__not_in parameters. I think this is the reason why your query doesn't retrieve any posts in the first place.

  • taxonomy parameter doesn't exist. There was a tax parameter before for sending the taxonomy slug but it has been deprecated since version 3.1. If you want to query your posts depending of a taxomy use the tax_query parameter instead.

0
votes

I would use a tax_query instead of the way you are trying to get posts by categories in your post args. Also posts_per_page replaced showposts but its probably not that critical. Try this out:

$args = array(
   'posts_per_page' => -1,
   'ignore_sticky_posts' => 1,
   'post_type'=> 'biology', 
   'tax_query' => array(
       array(
       'taxonomy' => 'teachres-categorie',
       'field' => 'term_id',
       'terms' => $category->term_id,
       ),
    )
);

$posts = get_posts($args);

These args will query all posts that have the term_id ( which is the id of the category inside the loop) retrieving all posts within that category term.

Also @vard makes a good point about the category parameter which I think answers your question about why it may not work.