I am trying to sort a custom wp_query using the date format YYYYMMDD (php: date(‘Ymd’) ).
This will not be regarded as date field, but rather as a numeric one stored in custom field.
My WP-QUERY code is the following:
$today = date("Ymd", strtotime(current_time('mysql')));
$args = array(
"post_type" => "post",
"post_status" => "publish",
"posts_per_page" => -1,
"cat" => 13,
"meta_query" => array(
array(
"key" => "event-date",
'type' => 'NUMERIC',
'value' => $today,
"compare" => ">="
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$eventsbody = new WP_Query($args);
var_dump($eventsbody->request);
upon the dump it produces the following code:
"SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) LEFT JOIN wp_icl_translations t ON wp_posts.ID = t.element_id AND t.element_type LIKE 'post_%' LEFT JOIN wp_icl_languages l ON t.language_code=l.code AND l.active=1 WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (13) ) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish') AND ( (wp_postmeta.meta_key = 'event-date' AND CAST(wp_postmeta.meta_value AS DATE) >= '20140326') ) AND (t.language_code='el' OR t.language_code IS NULL ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC "
The problem is, that it disregards the meta_value_num ordering and does the order by wp_posts.post_date. Is it a wp3.8.1 problem?
I think your query arguments are missing
meta_key
, which in this case should be'event-date'
. You need it outside the meta query for orderby to work. Try this: