What is the difference in a query when using a fieldname as a meta key vs orderby field?

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?

1 Answer
1

I 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.org/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!

Leave a Comment