带两个元查询的ORDER BY META_KEY

时间:2013-01-07 作者:Christopher

这应该很简单,但我无法找到一个正确语法的好例子。

我想按指定的meta\\u键和两个meta\\u查询排序。问题是query\\u帖子似乎不喜欢有两个带有meta\\u键的meta\\u查询。如果我取出一个meta\\u查询,代码就会工作,或者如果我删除meta\\u键并将orderby更改为title,而不使用meta\\u查询,代码也会工作。

有没有办法通过两个meta\\u查询按meta\\u键排序?

    $args2 = array(
        \'meta_key\' => \'_count-views_all\',
        //\'meta_value\' => $id,
        \'orderby\' => \'meta_value_num\',
        \'order\' => $sortOrder,
        \'posts_per_page\' => 9,
        \'paged\' => $paged,
        \'meta_query\' => array(  
                    \'relation\' => \'OR\',
                    array(
                        \'key\' => \'contributorid1\',
                        \'value\' => $id,
                        \'compare\' => \'=\'
                        ),

                    array(
                        \'key\' => \'contributorid2\',
                        \'value\' => $id,
                        \'compare\' => \'=\'
                        )
                    )
    );
    $posts = query_posts($args2); 
}

1 个回复
SO网友:Pontus Abrahamsson

您可以使用3.1版以来的WP\\U查询和meta_query.

$args = array(
    \'meta_key\' => \'_count-views_all\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => $sortOrder,
    \'posts_per_page\' => 9,
    \'paged\' => $paged,
    \'meta_query\' => array(
        \'relation\'  => \'OR\'
        array(
            \'key\'     => \'contributorid1\',
            \'value\'   => $id,
            \'compare\' => \'=\'
        ),
        array(
            \'key\'     => \'contributorid2\',
            \'value\'   => $id,
            \'compare\' => \'=\'
        )
    )
);

$query = new WP_Query( $args );

结束

相关推荐

Ordering posts by metadata

在我的插件中,我使用以下说明获取帖子列表:$args = array( \'numberposts\' => -1, \'offset\' => 0, \'meta_query\' => array( array( \'key\' => \'metadata1\', \'value\' => \'80\',