You don’t have to determine what type it is to query it, you just have to set the proper arguments to override defaults. First off, we’ll use WP_Query
to do additional queries instead of query_posts
.
If we only set the p
argument, post_type
defaults to post
, so we won’t get any pages:
$query = new WP_Query(
array(
'p' => 13
)
);
If we just add post_type
and set it to any
, we’ll get whatever post ID 13 is, whether it’s a post, page, attachment, etc..
$query = new WP_Query(
array(
'post_type' => 'any',
'p' => 13
)
);
If you have some post IDs and need to query all of them, you can get rid of the foreach loop and just do a single query for all the posts at once with the post__in
argument:
$query = new WP_Query(
array(
'post_type' => 'any',
'post__in' => array( 1, 2, 13, 42 )
)
);
Related Posts:
- Some doubts about how the main query and the custom query works in this custom theme?
- WP_Query vs get_posts
- Order posts by ID in the given order
- Get the number of posts from the current page results
- WordPress Custom Query to show posts from last x years
- Query Custom Meta Value with Increment
- Trying to list out child pages with WP_Query
- How to query ‘posts_per_page’ to display a different blog posts index template?
- 2 loops on page – one with orderby rand second orderby date
- Query posts by specific word on title
- Five posts from a category in footer
- Multiple loops without repeating content
- WP_Query condition affects posts_per_page count
- Serve specific “template” page at any URL that ends in its slug
- Some doubts about how the main query and the custom query works in this custom theme?
- Display custom tags on pages that have a specific page parent
- How do I rewrite this loop as a new WP_Query style-loop?
- Include current post into loop
- How to provide meta_key array to wp_query?
- get_posts output always same post
- How to create custom query by keyword in post title?
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Conditional operator OR not working with custom fields
- Create a loop on my pages with new “WP_Query”
- Get posts that do not have the same tags as current
- Should I reset $wp_query?
- Display specific page (that is child) content on parent page
- 2 queries with counters
- Compare “Main” post ID to ID inside wp_query loop
- Paginate pages with dynamic query
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- How to query for a page, get data, then query for child pages of that page
- Custom template for password protected page
- Custom query does not find pages
- Magazine style frontpage with multiple categories/loops and no duplicate posts
- How to use custom page for all posts with custom url, call another directory?
- Duplice post with standard WP loop – fixed by using query_posts() instead
- How do I stop the same post showing multiple times in a archive?
- Custom query for custom post type not getting correct post ID
- Problem with my loops
- WordPress For Loop Prints Unwanted Extra Paragraph Element
- display the children of the post using the current page as the main parent
- pages shortcode filtering by category
- How to order posts, that have already been filtered by custom taxonomy, by their category names?
- How to add in WP_Query to every 3 posts displayed? [duplicate]
- query_posts() doesn’t seem to be called in my page
- Some doubts about how the main query and the custom query works in this custom theme?
- Retrieving category pages from subcategory returns empty sets
- Ordering Posts by parent category, name ascending
- Issue in If else condition [closed]
- Issue with WP_Query (need a array of selected ID’s)
- Converting an existing query_posts to WP_Query
- get contents and permalink from a specified page
- Multiple loop for “featured” items returns wrong posts
- WordPress pagination returns the same posts
- Show number of posts AND number on current page (cannot make it work)
- after refresh the id that shows correct in first time click, changes to 1
- Loop within a loop?
- Merging a complex query with post_rewind and splitting posts into two columns
- How do I create my own nested meta_query using posts_where / posts_join?
- meta_query not working properly
- How to get any tag ID
- Targeting specific pages in the loop
- Query sticky posts with thumbnails
- Show all parents and children in custom post type in right order
- Why is this coming back as null? Thats wrong. There is one post
- Separate Posts and Custom Post Type in Custom Taxonomy archive template
- Ignore post by meta value in the main query
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- How to retrieve current page WP_Query arguments?
- How can I get an array of all IDs from the current queried object?
- Order a query result by a numeric meta key even if it does not exist, but put the posts with meta key first
- Random loop with code to prevent duplicate output returns no output at all from time to time
- How to use offset in WP_Query
- How to build a WP_Query using mulitple tags and using AND or OR operator between them
- different for loop if screen resolution < 1000px
- Transient api Caches confused
- How can I override one post and make it display content for another post?
- Get all posts which was posted on X Days WordPress
- How to get post ID in a Page?
- Exclude parent with child pages from WP_Query
- inserting content of 1 Post to in another with a template hierarchy
- Pagination for custom loop on custom page template is not displaying anything
- How to use ‘WP_Query’ or ‘query_posts’ to display content in a descending order
- Writing less unnecessary code with WordPress
- Only show tag with the same id as the post
- ACF: How can I publish values of ACF fields in a loop while using wp_query?
- Show a message if there are no active posts in category
- Display 3 levels of categories on page
- Paging works correctly on local version, but not live?
- Display three sequential posts on each page load, without repeating previous
- Modify query to exclude certain ids and certain parents and its corresponding children
- Pagination not working with WP_QUERY
- wp_query pagination links producing 404
- Display First posts without the default featured image
- Hierarchical List Pages as a table
- Pagination problem with multiple loops on the same page
- Can’t seem to get an else statement correct? [closed]
- Incorrect posts displayed on category page
- How do I split a large query with a semi-expensive function included into multiple smaller queries