按WP_QUERY中的自定义字段值进行特定排序

时间:2020-07-02 作者:ricardorios

我知道MySQL中存在这样一个查询:

SELECT * FROM mytable WHERE id IN (1,2,3,4) ORDER BY FIELD(id,3,2,1,4);
在WP\\u查询中是否有实现相同功能的方法?我想对meta\\u值进行如下排序:0、2、1、3、4

以下是我的参数:

$args_com_subset = array(
              \'posts_per_page\' => -1,
              \'post_type\' => \'company\',
              \'meta_key\' => \'company_tiers\',
              \'orderby\' => \'meta_value\',
              \'order\' => \'ASC\',           
              \'meta_query\' => array(
                    \'relation\'      => \'OR\',
                    array(
                        \'key\' => \'company_type\',
                        \'value\'    => \'full_methodology_n_subset_of_indicators\',
                        \'compare\'    => \'=\'
                    ),
                    array(
                        \'key\' => \'company_type\',
                        \'value\'    => \'subset_of_indicators\',
                        \'compare\'    => \'=\'
                    ) 
                ),
                \'tax_query\' => array(       
                    \'relation\'      => \'AND\',         
                    array(
                        \'taxonomy\' => \'company_year\',
                        \'field\' => \'slug\',
                        \'terms\' => \'2020\'
                    )
                )
            );

1 个回复
SO网友:Skatox

首先从args中删除order部分,并添加嵌套的meta\\u查询,如以下示例所示:

$args_com_subset = array(
    \'posts_per_page\' => -1,
    \'post_type\' => \'company\',  
    \'meta_key\' => \'company_tiers\', 
    \'meta_query\' => array(
        \'relation\'      => \'AND\',
        array(
            \'key\'     => \'company_tiers\',
            \'value\'   => array( 1,2,3,4 ),
            \'compare\' => \'IN\',
        ),
        array(
            \'relation\'      => \'OR\',
            array(
                \'key\' => \'company_type\',
                \'value\'    => \'full_methodology_n_subset_of_indicators\',
                \'compare\'    => \'=\'
            ),
            array(
                \'key\' => \'company_type\',
                \'value\'    => \'subset_of_indicators\',
                \'compare\'    => \'=\'
            ) 
        )
    ),
    \'tax_query\' => array(       
        \'relation\'      => \'AND\',         
        array(
            \'taxonomy\' => \'company_year\',
            \'field\' => \'slug\',
            \'terms\' => \'2020\'
        )
    )
);
然后,您必须添加一个过滤器来更改查询的排序部分,请记住在执行查询后删除此过滤器,以确保它不会影响任何其他查询:

add_filter( \'posts_orderby\', \'meta_key\' ); 
$posts = new WP_Query($args);
remove_filter( \'posts_orderby\', \'custom_sql_order\' ); 

function custom_sql_order($orderby) { 
    global $wpdb; 
    $orderby = "FIELDS(\'wp_postmeta.meta_value\', 3,2,1,4)";
    
    return $orderby;
} 

相关推荐

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

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