0

I am working in a project where I am using below code to limit number of posts in each category. But I can't figure out how to sort these categories by latest posts. My code is working fine but when I add argument in wp query array, it does not sort or it breaks. I want to order the category with most recent post (date).

Can anyone guide me?

<?php $terms=g et_terms( array( 'taxonomy'=>'category', 'hide_empty' => false, ) ); foreach($terms as $cat){ $cata_name = $cat->name; $term_id = $cat->term_id; ?>
<?php //echo '<h3>'.$catname[0]->cat_name.'</h3>'; ?>
<?php $catqueryy=n ew WP_Query ( 'cat='.$term_id. '&posts_per_page=5');$count=$ catqueryy->found_posts; ?>
codewario
  • 19,553
  • 20
  • 90
  • 159
Kxplorer
  • 28
  • 8

1 Answers1

0

You can add ORDER BY argument in wordpress as:

<?php
$args = array(
    'post_type'  => 'post', //your_post_type
    'orderby'    => 'date', //meta_value
    'meta_query' => array(array('key' => 'cat','value'=>$term_id)),
    'order'      => 'DESC',
    'posts_per_page' => 5,
);

$catqueryy = new WP_Query($args);
?>

You can explore the wordpress document: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

devpro
  • 16,184
  • 3
  • 27
  • 38