meta_query sorting by 2 keys

时间:2012-10-09 作者:Dameer

我需要按2个自定义字段值对(自定义)帖子进行排序。。。

自定义字段名称1:is_sponsored [值可以是10 ]

自定义字段名称2:sfp_date [timestamp aka当前发布日期(秒)]

发布其“is_sponsored值为1需要位于顶部,按排序sfp_date“在DESC结束订单。所有其他职位,其“is_sponsored“值为0应在下面列出-按降序排列(按)”sfp_date)以及。

我有这样的想法:

$sfp_query_args = array(
    \'tax_query\'   => array( 
        array( 
            \'taxonomy\' => \'sfp_posts\',
            \'terms\'    => array( 1, 5, 8 )
        )
    ),
    \'post_type\'   => \'sfpposts\',
    \'post_status\' => \'publish\',
    \'showposts\'   => 15,
    \'paged\'       => $paged,
    \'meta_key\'    => \'sfp_date\', 
    \'orderby\'     => \'meta_value_num\', 
    \'order\'       => \'DESC\', 
    \'meta_query\'  => array(
        \'key\'          => \'is_sponsored\',
        \'value\'        => 2,
        \'type\'         => \'NUMERIC\',
        \'compare\'      => \'<=\'
    )
);
$wp_q = new WP_Query( $sfp_query_args );
。。。但不起作用。有什么想法吗?

编辑们注意:这是一个小插件,可以显示查询的外观,因为我们可能没有任何可用的数据集来测试它。

<?php
/** Plugin Name: (#67600) Dump Query parts */
function wpse67600_dump_query_parts( $pieces )
{
    echo \'<pre>\'.var_export( $pieces, true ).\'</pre>\';
    return $pieces;
}
add_filter( \'posts_clauses\', \'wpse67600_dump_query_parts\' );
请在此处添加插件输出-使用“编辑”链接。

由Dameer编辑好的,在跟踪请求和众多解决方法之后,我想到了以下。。。

如果我稍微简化一下“$sfp\\u query\\u args”,结果接近所需的结果,但是,无法按原样对帖子进行排序。这是:

$sfp_query_args1 = array(
    \'tax_query\' => array( array( \'taxonomy\' => \'sfp_post_category\', \'terms\' => $cat_id_arr ) ),
    \'post_type\' => \'sfpposts\',
    \'post_status\' => \'publish\',
    \'showposts\' => (int)$per_page,
    \'paged\' => $paged,
    \'meta_key\' => \'is_sponsored\', 
    \'orderby\' => \'meta_value date\'
);
orderby有两个属性:meta\\u value和date*/ul>So$wpdb->查询中包含上述参数的请求如下所示:

SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.ID 
FROM $wpdb->posts 
INNER JOIN $wpdb->term_relationships 
ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
INNER JOIN $wpdb->postmeta 
ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) 
WHERE 1=1 
AND $wpdb->posts.post_type = \'sfpposts\' 
AND ($wpdb->posts.post_status = \'publish\') 
AND ($wpdb->postmeta.meta_key = \'is_sponsored\' ) 
GROUP BY $wpdb->posts.ID 
ORDER BY $wpdb->postmeta.meta_value, $wpdb->posts.post_date DESC 
LIMIT 0, $per_page
最后,为了能够按照meta\\u值进行排序,查询应该只设置一个细微的差别:

SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.ID 
FROM $wpdb->posts 
INNER JOIN $wpdb->term_relationships 
ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
INNER JOIN $wpdb->postmeta 
ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) 
WHERE 1=1 
AND $wpdb->posts.post_type = \'sfpposts\' 
AND ($wpdb->posts.post_status = \'publish\') 
AND ($wpdb->postmeta.meta_key = \'is_sponsored\' ) 
GROUP BY $wpdb->posts.ID 
ORDER BY $wpdb->postmeta.meta_value [!ORDER MISSING!], $wpdb->posts.post_date DESC 
LIMIT 0, $per_page
请指出[!缺少订单!]占位符。我想上面应该解释一下问题到底发生在哪里。

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

好的,最后的解决方法是拆分查询:

$sfp_query_args = array(
    \'tax_query\' => array( array( \'taxonomy\' => \'sfp_post_category\', \'terms\' => $cat_id_arr ) ),
    \'meta_key\' => \'is_sponsored\',
    \'post_type\' => \'sfpposts\',
    \'post_status\' => \'publish\',
    \'showposts\' => (int)$per_page,
    \'paged\' => $paged
);
。。。并使用“posts\\u orderby”过滤器修改订单部分:

add_filter( \'posts_orderby\', \'sfp_modify_orderby\' );
function sfp_modify_orderby( $orderby ) {
    if( !is_admin() && is_tax( \'sfp_post_category\' ) ) {
        global $wpdb;
        $orderby = " $wpdb->postmeta.meta_value DESC, $wpdb->posts.post_date DESC ";
    }
    return $orderby;
}
为了防止“posts\\u orderby”影响任何其他查询(边栏或页脚),最可能需要在页面上的循环之后删除过滤器。因此,在“functions.php”中还有一个函数:

function sfp_remove_orderby_filter() {
    remove_filter( \'posts_orderby\', \'sfp_modify_orderby\' );
}
。。。在使用我们的查询丢弃过滤器的页面上:

if( have_posts() ) : while( have_posts() ) : the_post();
    // code
endwhile;
else :
    // code
endif;

sfp_remove_orderby_filter();
希望它有意义!

SO网友:Md Toufiqul Islam

我正在编写您的查询,并稍加修改。我希望这能有所帮助。

$sfp_query_args = array(
    \'tax_query\' => array( array( \'taxonomy\' => \'sfp_posts\', \'terms\' => array( 1, 5, 8) ) ),
    \'post_type\' => \'sfpposts\',
    \'post_status\' => \'publish\',
    \'showposts\' => 15,
    \'paged\' => $paged, 
    \'meta_key\'=>\'sfp_date\', 
    \'meta_query\' => array(
    array(
        \'key\' => \'sfp_date\',
            \'type\' => \'NUMERIC\',
    ),
    array(
        \'key\' => \'is_sponsored\',
        \'value\' => \'2\',
        \'compare\' => \'<=\'
    )       
    ),
    \'orderby\' => \'meta_value_num\', 
    \'order\' => \'DESC\',
);
$wp_q = new WP_Query( $sfp_query_args );
请让我知道它是否有效:-)

结束

相关推荐