admin管理员组

文章数量:1122846

How do I use get_posts to filter against a single post id? (I don't want to use get_post because am using this in another function that could take other parameters and should return an array). The following methods are not working:

get_posts(array(
    'ID' => 12345,
));

get_posts(array(
    'p' => 12345,
));

get_posts(array(
    'post' => 12345,
));

get_posts(array(
    'post__in' => array(12345),
));

How do I use get_posts to filter against a single post id? (I don't want to use get_post because am using this in another function that could take other parameters and should return an array). The following methods are not working:

get_posts(array(
    'ID' => 12345,
));

get_posts(array(
    'p' => 12345,
));

get_posts(array(
    'post' => 12345,
));

get_posts(array(
    'post__in' => array(12345),
));
Share Improve this question edited Dec 10, 2021 at 18:17 tklodd asked Dec 10, 2021 at 17:38 tkloddtklodd 1296 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Nevermind, all of these methods work if I add the post_type parameter to the query:

get_posts(array(
    'ID' => 12345,
    'post_type' => array('any'),
));

get_posts(array(
    'p' => 12345,
    'post_type' => array('any'),
));

get_posts(array(
    'post' => 12345,
    'post_type' => array('any'),
));

get_posts(array(
    'post__in' => array(12345),
    'post_type' => array('any'),
));

This is a really stupid design decision; any post type should be assumed if the parameter is omitted.

本文标签: How to use getposts to filter against a single post id