I have tried this way but orderby and order not working on WP_Query class
$posts = new WP_Query(
array(
'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
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',
'menu_order'=>'ASC',
/*Other params*/
)
);
$query = new WP_Query($params);
This example working properly for me in WP Version_4.x
According to the docs to show posts ordered by date you should use date
.(But the default is date anyway)
"orderby (string | array) - Sort retrieved posts by parameter. Defaults to 'date (post_date)'. One or more options can be passed."
'orderby'=> 'date',
To show posts associated with certain type these are the valid types.So you must use on of them
https://codex.wordpress.org/Class_Reference/WP_Query
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With