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

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

Leave a Reply

Your email address will not be published. Required fields are marked *