Wordpress中阵列的多个帖子ID
问题描述:
我想显示3个特定帖子。Wordpress中阵列的多个帖子ID
问题:我的帖子ID来自以前的数组。
结果:它只显示第一个。
功能:从$ algoid(帖子ID)= 865,866
foreach($fav_author_list as $i => $item) {
$insert = get_user_favorites($item);
if (!is_array($insert[0])) {
$result = array_merge($result, $insert);
}
}
$algoid = implode(",", $result);
结果,877
我想显示的三个职位。
$myarray = array($algoid);
$args = array(
'post__in' => $myarray,
);
// The Query
$the_query = new WP_Query($args);
答
您不必为post__in
内爆您的$algoid
。由于您使用的implode
,你实际上是传递一个数组的字符串为您查询:
array('865, 866, 877'); // Items: 1
然而,WP_Query期待一个阵列的ID,而不是作为一个字符串:
array(865, 866, 877); // Items: 3
这里就应该是这样:
// Use your function to generate the array with the IDs
$algoid = array(865, 866, 877);
$args = array(
'post__in' => $algoid
);
更多有关WP_Query
:https://codex.wordpress.org/Class_Reference/WP_Query
post__in(array) - 使用post ID。指定要检索的帖子。注意如果您使用粘性帖子,无论您是否需要它们,它们都会被包含(前置!)在您检索的帖子中。要抑制此行为,请使用ignore_sticky_posts。
工作!谢谢 – user1708580
@ user1708580欢迎您:) –