Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To display the post order by modified date in wordpress

Tags:

post

wordpress

I have to display the posts oder by last modified date. So I used the code bellow.

$args = array(
    'post_type' => $post_type,
    'numberposts' => '2',
    'orderby' => 'modified',
    'order'=> 'ASC',
);
$the_query = new WP_Query( $args );

But I could not find any update in the above code. Should i use something else instead of 'orderby' => 'modified' in the argument.

like image 873
Ranjit Avatar asked Jan 21 '13 12:01

Ranjit


1 Answers

You should use DESC for order.

Try this:

 $the_query = new WP_Query( array(
     'post_type'   => $post_type,
     'numberposts' => '2',
     'orderby'     => 'modified',
     'order'       => 'DESC',
 ));

Using DESC will give you the latest post first(descending order).

EDIT:

As Andrew commented, the default value for order is DESC and can thus be omitted from the code:

 $the_query = new WP_Query( array(
     'post_type'   => $post_type,
     'numberposts' => '2',
     'orderby'     => 'modified',
 ));
like image 69
RRikesh Avatar answered Oct 31 '22 09:10

RRikesh