按元关键字排序不起作用

时间:2020-05-15 作者:JoaMika

我正在使用此代码,但似乎按meta\\u键排序br_category 第一个和br_name 第二,没有产生正确的结果

$args = array(
   \'post_type\' => \'brands\',
   \'meta_query\' => array( 
      array( 
        \'key\' => \'br_type\', 
        \'value\' => \'Aviation\'
      ), 
      array( 
        \'key\' => \'br_category\' 
      ), 
      array( 
        \'key\' => \'br_name\' 
      ) 
   ), 
   \'posts_per_page\' => -1, 
   \'orderby\' => [ \'br_category\' => \'ASC\', \'br_name\' => \'ASC\' ], 
   \'order\' => \'ASC\', 
   \'fields\' => \'ids\'
);
我使用的语法是否不正确?

2 个回复
最合适的回答,由SO网友:simongcc 整理而成

当前查询

$args = array(
   \'post_type\' => \'brands\',
   \'meta_query\' => array( 
      array( 
        \'key\' => \'br_type\', 
        \'value\' => \'Aviation\'
      ), 
      array( 
        \'key\' => \'br_category\' 
      ), 
      array( 
        \'key\' => \'br_name\' 
      ) 
   ), 
   \'posts_per_page\' => -1, 
   \'orderby\' => [ \'br_category\' => \'ASC\', \'br_name\' => \'ASC\' ], 
   \'order\' => \'ASC\', 
   \'fields\' => \'ids\'
);
生成此SQL查询

\'SELECT wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )  INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) WHERE 1=1  
    AND ( 
        ( wp_postmeta.meta_key = \\\'br_type\\\' AND wp_postmeta.meta_value = \\\'Aviation\\\' ) 
    AND 
    mt1.meta_key = \\\'br_category\\\' 
    AND 
    mt2.meta_key = \\\'br_name\\\'
    ) AND wp_posts.post_type = \\\'brands\\\' AND (wp_posts.post_status = \\\'publish\\\' OR wp_posts.post_status = \\\'acf-disabled\\\' OR wp_posts.post_author = 1 AND wp_posts.post_status = \\\'private\\\') GROUP BY wp_posts.ID  \'
这意味着它不添加任何order by子句,因为它不理解。

下面是按多个元键值排序的正确方法

$args = array(
        \'post_type\' => \'brands\',
        \'meta_query\' => array( 
            // \'relation\' => \'AND\', // default is AND
            \'br_type_clause\' => array( 
                \'key\' => \'br_type\', 
                \'value\' => \'Aviation\',
            ), 
            \'br_category_clause\' => array( 
                \'key\' => \'br_category\',
            ), 
            \'br_name_clause\' => array( 
                \'key\' => \'br_name\',
            ) 
        ), 
        \'posts_per_page\' => -1, 

        \'meta_key\' => array( \'br_category\', \'br_name\', \'br_type\' ),
        // add sql: wp_postmeta.meta_key IN (\'br_category\',\'br_name\',\'br_type\') 

        \'orderby\' => array(
            \'br_type_clause\' => \'ASC\', 
            \'br_category_clause\' => \'ASC\',
            \'br_name_clause\' => \'ASC\',
            \'name\' => \'ASC\', // illustrative purpose, could use post key
        ), 
        // \'order\' => \'ASC\', // it is ignored with above orderby override
        \'fields\' => \'ids\'
    ),
产生SQL

SELECT   wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )  INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id )  INNER JOIN wp_postmeta AS mt3 ON ( wp_posts.ID = mt3.post_id ) WHERE 1=1  AND ( 
  wp_postmeta.meta_key IN (\'br_category\',\'br_name\',\'br_type\') 
  AND 
  ( 
    ( mt1.meta_key = \'br_type\' AND mt1.meta_value = \'Aviation\' ) 
    AND 
    mt2.meta_key = \'br_category\' 
    AND 
    mt3.meta_key = \'br_name\'
  )
) AND wp_posts.post_type = \'brands\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'acf-disabled\' OR wp_posts.post_author = 1 AND wp_posts.post_status = \'private\') GROUP BY wp_posts.ID ORDER BY CAST(mt1.meta_value AS CHAR) ASC, CAST(mt2.meta_value AS CHAR) ASC, CAST(mt3.meta_value AS CHAR) ASC, wp_posts.post_name ASC 
参考号:WP_Query 带截面‘orderby’ with multiple ‘meta_key’s

SO网友:shanebp

Have you tried:

\'orderby\' => array( \'br_category\' => \'ASC\', \'br_name\' => \'ASC\' ), 

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post