我有一种不同的发布订单我试图回复我无法在任何地方找到答案。我使用wp_query尝试按元值列表排序帖子。然而,没有标准的orderby选项做我想要实现的目标。
我的元值存储为逗号列表字符串,如下所示:
1,5,10,3,21,6
然后我抓住这些值并将它们分解为如下数组:
$banner_nums = get_option('banner_numbers');
$nums_array = explode( ',', $banner_nums );
我做了一个标准的wp_query,但问题是,我试图按照我的$ banner_nums变量中的实际顺序来命令显示这些帖子,而不是通过任何正常的wp_query命令,例如' ID& #39;,' date',' title',' meta_value'等
显示如下:
1st:发布meta_value = 1
第二篇:meta_value = 5
的帖子第3名:帖子,meta_value = 10
第4名:发布meta_value = 3
第5名:帖子,meta_value = 21
6th:post with meta_value = 6
例如,如果我想显示meta_value = 6作为第二篇文章的帖子,我将列表更改为:1,6,5,10,3,21。
这是我的查询,抓住我的帖子很好,我只是无法让他们正确订购。
$slider_args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'nopaging' => true
);
$my_query = array();
foreach( $nums_array as $k => $v ) {
$my_query[$k]['key'] = 'property_id';
$my_query[$k]['value'] = $v;
$my_query[$k]['compare'] = '=';
}
$slider_args['meta_query'] = $my_query;
$slider_args['meta_query']['relation'] = 'OR';
$slider_query = new WP_Query( $slider_args );
if ( $slider_query->have_posts() ) {
stuff...
}
打印我的$ slider_args看起来像这样,看起来不错:
[post_type] => property
[posts_per_page] => -1
[nopaging] => 1
[meta_query] => Array
(
[0] => Array
(
[key] => property_id
[value] => 1
[compare] => =
)
[1] => Array
(
[key] => property_id
[value] => 5
[compare] => =
)
[2] => Array
(
[key] => property_id
[value] => 10
[compare] => =
)
[3] => Array
(
[key] => property_id
[value] => 3
[compare] => =
)
[4] => Array
(
[key] => property_id
[value] => 21
[compare] => =
)
[5] => Array
(
[key] => property_id
[value] => 6
[compare] => =
)
[relation] => OR
)
我无法弄清楚如何以正确的顺序显示帖子
1,5,10,3,21,6
我希望我有意义吗?非常感谢任何帮助。
答案 0 :(得分:2)
我自己试图为你解决这个问题:>
显然从WP 3.5开始,您可以在创建WP_Query时指定'post__in'的'orderby'参数
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
基本上我认为你可以做的就是获取具有你需要的元值的帖子ID,并将这些ID按照它们在元值数组中指定的顺序放入数组中。然后,如果你在该数组上执行WP_Query,请传入arg:
'orderby' => 'post__in'
现在,正如另一位评论者指出的那样,这可能是一个相当昂贵的操作,因此您希望将结果缓存到瞬态并使用该输出而不是每次加载页面时运行此查询:
答案 1 :(得分:0)
感谢Greg的输入。我正在使用' orderby' => ' post__in" arg,但没有考虑过必须添加帖子ID数组。我不得不添加额外的' post__in' =>数组(ID),以便orderby正常工作。所以我必须像你说的那样做,并添加一个昂贵的查询来获取这些ID。我的最终代码如下:
$banner_nums = get_option('banner_numbers');
$numbers = explode( ',', $banner_nums );
// ** new code
$wpIds = array();
// need post ids to keep the sort order same as in string above
foreach( $numbers as $num) {
$args = array(
'post_type' => 'property',
'meta_query' => array(
array(
'key' => 'property_id',
'value' => $num
)
)
);
$getPosts = new WP_Query($args);
if( $getPosts->have_posts() ) {
while( $getPosts->have_posts() ) {
$getPosts->the_post();
// builds array of posts IDs in same order as $numbers array above.
array_push($wpIds, get_the_ID());
} // end while
}
}
// ** end new code
$slider_args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'nopaging' => true,
'post__in' => $wpIds, // ** new code
'orderby' => 'post__in'
);
$my_query = array();
foreach( $numbers as $k => $v ) {
$my_query[$k]['key'] = 'property_id';
$my_query[$k]['value'] = $v;
$my_query[$k]['compare'] = '=';
}
$slider_args['meta_query'] = $my_query;
$slider_args['meta_query']['relation'] = 'OR';
$slider_query = new WP_Query( $slider_args );
是的,这是一个昂贵的查询,但对于我的情况,使用它来构建2-6张幻灯片的主页滑块,我可以处理它。