0

So I've read all the stack exchange posts on this, and I feel like what I have, based on what I've read, should be working. I'm a little confused as to why it isn't.

$args = array(
    'post_type' => 'event',
    'meta_key' => 'events_startdate',
    'order_by' => 'meta_value',
  );

  $post_query = new WP_Query($args);
  if($post_query->have_posts() ) {
    while($post_query->have_posts() ) {

I should mention that in my loop I have echo'd out my meta_key to make sure it's returning the correct info:

<?php echo get_post_meta(get_the_ID(), 'events_startdate', true); ?>

And by my estimation it is. It's spitting out a nice round number like this: 1545091200

This looks to me like it should work, but it doesn't. Anyone see anything glaring that I'm missing?

15
  • 1
    try this 'orderby' => 'meta_value_num', – vikrant zilpe Nov 29 '18 at 16:25
  • I have tried that already as well and not getting anything different in my order. – Dale Spiteri Nov 29 '18 at 16:29
  • Just to add a note, I'm echoing out the value on each card just so I can make sure each has the correct meta_key value, and the order by ascending numbers is coming out: 2, 3, 4, 1 So its ordering them by post creation despite being told to order by meta_key My only thought is the meta value isn't coming out correctly - but in my test echo, it looks like it is. – Dale Spiteri Nov 29 '18 at 16:31
  • 1
    'orderby' => 'meta_value_num', 'order' => 'DESC' – vikrant zilpe Nov 29 '18 at 16:31
  • Is your round number an example of the meta value? Or is it stored in some other format? The format is rather important here as anything other than a basic number won't necessarily be interpreted the way you think it will. – WebElaine Nov 29 '18 at 16:34
0

Had same issue. Have spent like few hours searching for the problem and it turned out to be just a typo: instead of incorrect order_by you should use orderby - without the underscore. So, this should work:

$args = array(
    'post_type' => 'event',
    'meta_key'  => 'events_startdate',
    'orderby'   => 'meta_value',
  );

  $post_query = new WP_Query( $args );
  if( $post_query->have_posts() ) {
    while( $post_query->have_posts() ) {...}
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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