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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

The 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