Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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:

$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" => ">="
  )
  ),
  'meta_key' => 'event-date', // meta_key added
  'orderby' => 'meta_value_num',                            
  'order' => 'ASC'
  );
  $eventsbody = new WP_Query($args); 
share|improve this answer
    
True, it was the missing piece. I assumed it took the meta_key using the meta_query arguments. Thank you for your help! –  Panagiotis Mar 26 at 12:20
    
Panagiotis, I thought so as well, but since meta_query can house multiple queries, and hence multiple keys, you need to specify which you want to use. –  Eric Holmes Mar 26 at 13:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.