Column meta_key
is used for filtering and partially for ordering. Why partially? It is used for ordering only in combination with meta_value
. Columns meta_key
and meta_value
are connected together like column name
and column value
in traditional table. For raw SQL select it would be possible to order posts by meta_key
but from a practical point of view, do you order your SQL selects by column names?
Let’s assume that we have custom post type event
with meta_key
_speakers
and _attendants
. I don’t see point of ordering events by meta_key
but I see point of ordering by _speakers
value or _attendants
value. To do this you have to make such a query.
$query = new WP_Query(array(
'post_type' => 'event',
'meta_key' => '_speakers',
'orderby' => 'meta_value_num',
));
or
$query = new WP_Query(array(
'post_type' => 'event',
'meta_key' => '_attendants',
'orderby' => 'meta_value_num',
));