我需要按2个自定义字段值对(自定义)帖子进行排序。。。
自定义字段名称1:is_sponsored
[值可以是1
或0
]
自定义字段名称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
请指出[!缺少订单!]占位符。我想上面应该解释一下问题到底发生在哪里。