wp_query sort by meta value code example
Example 1: get post order by meta value int
$defaults = array(
'numberposts' => -1,
'post_type' => 'agent_listing',
'meta_key' => 'oepl_order_no',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'suppress_filters' => false
);
'orderby' => 'meta_value_num',
'orderby' => 'meta_value',
Example 2: order by meta_query wordpress
$q = new WP_Query(
array(
'meta_key' => 'population',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'state',
'value' => 'Wisconsin',
),
array(
'key' => 'timezone',
'value' => 'central',
)
),
array(
'key' => 'city',
'compare' => 'EXISTS',
)
)
)
);