The post date isn’t meta. You only orderby
meta_value
if the value is a custom field stored in the wp_postmeta
table. The publication date is in the wp_posts
table and part of the core post data.
If you want to order by date you set orderby
to date
, and don’t set any of the meta_
vars.
However, setting orderby
and order
with query strings is supported automatically, without pre_get_posts
. For example, if you want to view Make WordPress Core’s posts in reverse alphabetical order, you can just go to https://make.wordpress.org/core/?order=DESC&orderby=title and it’ll work (no posts starting with Z yet!).
So all your function needs to do is handle the property_price2
and area2
orderby values, which WordPress won’t understand on its own.
function wpse_139657_orderby() {
if ( isset( $_GET['orderby'] ) ) {
if ( in_array( $_GET['orderby'], ['property_price2', 'area2'] ) ) {
set_query_var( 'orderby', 'meta_value_num' );
set_query_var( 'meta_key', $_GET['orderby'] );
}
}
}
add_filter( 'pre_get_posts', 'wpse_139657_orderby' );