我有一个帖子ID为的字符串:43,23,65
.<我希望我可以get_posts()
并使用ID为的字符串作为参数。
但我找不到任何按ID检索多篇帖子的函数。
我真的要做一个WP_query
?
我还看到有人提到使用tag_in
- 但我找不到关于这方面的任何文件。
我有一个帖子ID为的字符串:43,23,65
.<我希望我可以get_posts()
并使用ID为的字符串作为参数。
但我找不到任何按ID检索多篇帖子的函数。
我真的要做一个WP_query
?
我还看到有人提到使用tag_in
- 但我找不到关于这方面的任何文件。
您可以使用get_posts()
因为它的参数与WP_Query
.
要传递ID,请使用\'post__in\' => array(43,23,65)
(仅接受数组)。
类似于:
$args = array(
\'post__in\' => array(43,23,65)
);
$posts = get_posts($args);
foreach ($posts as $p) :
//post!
endforeach;
我还设置了post_type
和posts_per_page
只是为了更好的衡量。如果无法实现上述功能,请确保添加post_type
:
$args = array(
\'post_type\' => \'pt_case_study\',
\'post__in\' => array(2417, 2112, 784)
);
$posts = get_posts($args);
如果要通过其ID获取所有帖子(无论帖子类型如何),请使用以下选项:
$args = [
\'post_type\' => get_post_types(),
\'post__in\' => [ 43, 23, 65 ]
];
$posts = get_posts($args);
甚至更短:$args = [
\'post_type\' => \'any\',
\'post__in\' => [ 43, 23, 65 ]
];
$posts = get_posts($args);