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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0