I'm trying to figure out the actual difference in what is happening between these two args setups for a wp_query
$today= date('Ymd');
$homepageEvents = new WP_Query(array(
'post_type' => 'event',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'meta_key' => 'event_date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
'type' => 'numeric'
)
)
));
vs
$today= date('Ymd');
$homepageEvents = new WP_Query(array(
'post_type' => 'event',
'posts_per_page' => -1,
'orderby' => 'event_date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
'type' => 'numeric'
)
)
));
The only diffence is in one, it is setup as a orderby
using a parameter of meta_value_num
which then requires adding a meta key set to the field name. In the other the orderby
just uses the fieldname itself.
Does this result in the same query or am I asking 2 questions that just happen to have the same answer?
I'm trying to figure out the actual difference in what is happening between these two args setups for a wp_query
$today= date('Ymd');
$homepageEvents = new WP_Query(array(
'post_type' => 'event',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'meta_key' => 'event_date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
'type' => 'numeric'
)
)
));
vs
$today= date('Ymd');
$homepageEvents = new WP_Query(array(
'post_type' => 'event',
'posts_per_page' => -1,
'orderby' => 'event_date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
'type' => 'numeric'
)
)
));
The only diffence is in one, it is setup as a orderby
using a parameter of meta_value_num
which then requires adding a meta key set to the field name. In the other the orderby
just uses the fieldname itself.
Does this result in the same query or am I asking 2 questions that just happen to have the same answer?
Share Improve this question asked Apr 16, 2019 at 17:17 presswordpressword 3801 gold badge4 silver badges13 bronze badges1 Answer
Reset to default 2I would saythose are two different cases.
If I understood codex correctly and didn't miss anything, meta_value
and meta_value_num
are valid values for orderby
, but using meta field name / key directly isn't. You would need to add the field name as meta_key
parameter to the query args.
orderby
defaults to date
which I think might cause similar results for the two setups.
Related codex entry https://codex.wordpress/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
If meta key is a valid value for orderby
, then that is something new to me. Thanks!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745582269a4634323.html
评论列表(0条)