tarikul05 tarikul05 - 1 year ago 103
MySQL Question

how to order by date in WP_Query ?

I have tried this way but orderby and order not working on WP_Query class

$posts = new WP_Query(
'post_type'=> 'block_code',
'orderby'=> 'post_date',
'order' => 'DESC'

always it return orderby=> 'menu_order' and order='ASC'.

Note: if i use param in url as orderby=date&order=ASC then it works fine But i need as argument of WP_Query.

Thanks in advance

Answer Source

You can set multiple parameters for orderby in your WP_Query(). Like date,title,menu_order etc.

Here is the Order & Orderby Parameters

Try this example

$params = array(
    'post_type' =>'block_code',
    'orderby'   => array(
      'date' =>'DESC',
      /*Other params*/
$query = new WP_Query($params);

This example working properly for me in WP Version_4.x

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download