admin管理员组文章数量:1291102
I've got a string with post ID's: 43,23,65
.
I was hoping I could use get_posts()
and use the string with ID's as an argument.
But I can't find any functions for retrieving multiple posts by ID.
Do I really have to do a WP_query
?
I've also seen someone mention using tag_in
- but I can't find any documentation on this.
I've got a string with post ID's: 43,23,65
.
I was hoping I could use get_posts()
and use the string with ID's as an argument.
But I can't find any functions for retrieving multiple posts by ID.
Do I really have to do a WP_query
?
I've also seen someone mention using tag_in
- but I can't find any documentation on this.
3 Answers
Reset to default 56You can use get_posts()
as it takes the same arguments as WP_Query
.
To pass it the IDs, use 'post__in' => array(43,23,65)
(only takes arrays).
Something like:
$args = array(
'post__in' => array(43,23,65)
);
$posts = get_posts($args);
foreach ($posts as $p) :
//post!
endforeach;
I'd also set the post_type
and posts_per_page
just for good measure.
If you can't get the above to work, make sure you add post_type
:
$args = array(
'post_type' => 'pt_case_study',
'post__in' => array(2417, 2112, 784)
);
$posts = get_posts($args);
If you want to get all the posts by their IDs (regardless of post type) use this:
$args = [
'post_type' => get_post_types(),
'post__in' => [ 43, 23, 65 ]
];
$posts = get_posts($args);
Or even shorter:
$args = [
'post_type' => 'any',
'post__in' => [ 43, 23, 65 ]
];
$posts = get_posts($args);
本文标签: How do I get posts by multiple post ID39s
版权声明:本文标题:How do I get posts by multiple post ID's? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515050a2382834.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_posts()
codex.wordpress/Template_Tags/get_posts ? – Michael Commented Dec 31, 2011 at 11:56