最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - WP_Query by just the id?

matteradmin4PV0评论

In the codex for WP_Query I see you can query by page_id=7 for pages or by p=7 for posts. Is there a way to get a post of any post type by ID? Like id=7 that will get it no matter if it's a page, post or custom post type?

I can only get WP_Query with p=7 to work if I add &post_type=customposttype. Is there a way to get it from the ID regardless of the post type?

In the codex for WP_Query I see you can query by page_id=7 for pages or by p=7 for posts. Is there a way to get a post of any post type by ID? Like id=7 that will get it no matter if it's a page, post or custom post type?

I can only get WP_Query with p=7 to work if I add &post_type=customposttype. Is there a way to get it from the ID regardless of the post type?

Share Improve this question edited Feb 9, 2017 at 14:25 Sven 3,6841 gold badge35 silver badges48 bronze badges asked Jun 15, 2011 at 0:35 supertruesupertrue 3,01610 gold badges46 silver badges60 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 98

any should retrieve any type:

$args = array(
  'p'         => 42, // ID of a page, post, or custom type
  'post_type' => 'any'
);
$my_posts = new WP_Query($args);

Note the description of any in the documentation:

'any' - retrieves any type except revisions and types with 'exclude_from_search' set to true.

For more information, have a look at the documentation of WP_Query.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far