2

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();

1 Answer 1

3

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();
2
  • I have an error. On the string 'posts_per_page' => 1 unexpected T_DOUBLE_ARROW
    – okonik
    Commented Mar 24, 2013 at 14:40
  • @okonik i fixed it.
    – Bainternet
    Commented Mar 24, 2013 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.