admin管理员组文章数量:1123507
I have this query:
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
'tipo_pagina_clause' => array(
'key' => 'tipo_pagina',
'value' => 'prodotto',
'compare' => '='
),
'tipo_prodotto_clause' => array(
'key' => 'tipo_prodotto',
'compare' => 'EXISTS',
),
array(
'key' => 'brand',
'value' => get_the_id(),
'compare' => 'LIKE',
),
),
'orderby' => array(
'tipo_prodotto_clause' => 'ASC',
'post_title' => 'asc',
),
);
// query
$prodotti_brand = new WP_Query( $args );
Everything works fine, but what I want is 'post_title' = > 'rand', but the 'rand' seems not to work. What am I doing wrong?
I have this query:
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
'tipo_pagina_clause' => array(
'key' => 'tipo_pagina',
'value' => 'prodotto',
'compare' => '='
),
'tipo_prodotto_clause' => array(
'key' => 'tipo_prodotto',
'compare' => 'EXISTS',
),
array(
'key' => 'brand',
'value' => get_the_id(),
'compare' => 'LIKE',
),
),
'orderby' => array(
'tipo_prodotto_clause' => 'ASC',
'post_title' => 'asc',
),
);
// query
$prodotti_brand = new WP_Query( $args );
Everything works fine, but what I want is 'post_title' = > 'rand', but the 'rand' seems not to work. What am I doing wrong?
Share Improve this question asked Mar 18, 2019 at 12:59 globdugglobdug 32 silver badges5 bronze badges1 Answer
Reset to default 1The short answer is "you can't do that." Take a look at the Codex.
When using order
as an array the value can either be ASC
or DESC
. If you want to set rand
you would do so as a key only with orderby
. That being said, ordering by random will override any other orders, so you won't really get a benefit from ordering by tipo_prodotto_clause
first.
'order' => 'ASC',
'orderby' => 'rand',
Also make sure you take note of the required context for orderby rand:
Random order. You can also use 'RAND(x)' where 'x' is an integer seed value. Note an "order" parameter needs to be present for "orderby" rand to work.
A piece of advice: ordering randomly is highly discouraged. The query does not perform well in mysql and has the potential to break caching on a site for every request or not appear to work if caching is less flexible. In fact, WP Engine has banned its usage by default. Be careful using this. It's usually better to find an alternative solution.
本文标签: wp queryWPQuerymultiple orderby with a rand fielddoesn39t work
版权声明:本文标题:wp query - WP_Query - multiple orderby with a rand field, doesn't work 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736575786a1944853.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论