admin管理员组文章数量:1318973
In testing I am getting inconsistent results when experimenting with the meta_key
parameter on a fresh Wordpress install. I'm combining it with orderby
and/or meta_value
to understand ordering and filtering results.
Is the meta_key
parameter used for ordering search results as mentioned here or is it used for filtering as mentioned here? In no case have I seen Wordpress use it for both features, but it would apply to one or the other inconsistently.
I am aware that I could filter by meta_query
but that is not the scope of this question. I have examined the WP_Query->get_posts() method, and I see the exact steps used to construct the SQL (convoluted as it is) but the purpose of this question is to understand the intended behaviour and submit a patch if necessary.
I am testing using Wordpress version 4.9.1 (current up-to-date version), but I'm interested in all Wordpress 4.x versions and differences between them.
In testing I am getting inconsistent results when experimenting with the meta_key
parameter on a fresh Wordpress install. I'm combining it with orderby
and/or meta_value
to understand ordering and filtering results.
Is the meta_key
parameter used for ordering search results as mentioned here or is it used for filtering as mentioned here? In no case have I seen Wordpress use it for both features, but it would apply to one or the other inconsistently.
I am aware that I could filter by meta_query
but that is not the scope of this question. I have examined the WP_Query->get_posts() method, and I see the exact steps used to construct the SQL (convoluted as it is) but the purpose of this question is to understand the intended behaviour and submit a patch if necessary.
I am testing using Wordpress version 4.9.1 (current up-to-date version), but I'm interested in all Wordpress 4.x versions and differences between them.
Share Improve this question asked Dec 19, 2017 at 8:06 dotancohendotancohen 8391 gold badge9 silver badges20 bronze badges1 Answer
Reset to default 1Column 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',
));
本文标签: wp queryIs metakey used for orderingFilteringor both
版权声明:本文标题:wp query - Is meta_key used for ordering, filtering, or both? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737371386a1985413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论