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.

How can I post last posts from custom_taxonomy? I tried this like, but it doesn't works.

$terms=get_terms('tax_name');
$request=array();
 if(count($terms)>0)
 {
  foreach($terms as $t)
   {
       $request[]=$t->slug;
     }
  }
  query_posts(
  array(
    'tax_query' =>     array(
             'taxonomy' => 'tax_name',
             'field' => 'slug',
             'terms' => $request,
             )
    )
  );
  while ( have_posts() ):the_post();
share|improve this question

1 Answer 1

up vote 3 down vote accepted

First you really shouldn't use query_posts for that use get_posts or WP_Query and second tax_query accept an array of arrays

from the codex:

tax_query takes an array of tax query arguments arrays (it takes an array of arrays)

so:

$terms=get_terms('tax_name');
$request=array();
foreach((array)$terms as $t)
    $request[]=$t->slug;

$tax_q = new WP_Query(
    array(
        'post_type' => 'post',
        'posts_per_page' => 1,
        'tax_query' =>  array(
            array(
                'taxonomy' => 'tax_name',
                'field'    => 'slug',
                'terms'    => $request,
            )
        )
    )
);
while ( $tax_q->have_posts() ): $tax_q->the_post();
share|improve this answer
    
I have an error. On the string 'posts_per_page' => 1 unexpected T_DOUBLE_ARROW –  okonik Mar 24 '13 at 14:40
    
@okonik i fixed it. –  Bainternet Mar 24 '13 at 14:57
    
Big thanks to you! It works! –  okonik Mar 24 '13 at 15:05

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.