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
- Query_posts works when appending via AJAX call wp_query doesn’t?
- Multiple Rows in a using wp_query
- Use WP_Query or query_posts() or get_posts() for optimizing a site?
- How to use WP_Query() on single.php?
- Pagination on category.php and tag.php not working
- How to get posts published on the latest date?
- Using WP_Query and Query_post for the loop?
- sort and display posts by custom field (wp-query and the loop))
- difference between querying database and using the loop
- If have posts a week old, display, else display a different loop
- WP_Query – Adding “offset” posts to the end of the loop
- Stomping WP_Query in author archive to facilitate pagination with custom queries
- complex get_posts() query to select child pages
- Loop increase in while loop not working
- get_the_ID() in the footer returns wrong value
- posts archive page – closing WP_Query loop correctly [closed]
- Pagination not working on custom query on a page
- get term id from term name
- Create a page template for “top rated posts” but show full content and not just a list
- has_excerpt() not working inside wp_query
- All blog data on a page using custom query gives 404 for page 2 and onward
- wp_query is showing posts from other categories
- Daily drip of posts – based on user ‘startData’ – ordered oldest to newest
- How to add tax_query to $args with concatenation
- Meta query compare for ID’s greater than specific ID
- Exclude a Woocommerce product from WP_Query
- WP ForLoop to compare meta information of posts to determine what post to display
- AJAX Breaking Offset Argument In WP Query
- WP_QUERY wrong ammount of posts
- How to add sort order to incremented and paginated category loop
- Posts Query | Query to only show sub categories
- How does WP generate the default $query in WP_Query based on the URL?
- Continue or break the while loop
- How to filter query loop block with a search string from the query parameters
- inserting content of 1 Post to in another with a template hierarchy
- how to get category`s slug in WP_Query loop?
- Loop posts based on permalink term
- Save (and exclude) posts from loop to use in another loop on page
- Get all posts with empty meta_value
- Posts query according to meta box date
- How to split a post and intercalate elements from a loop
- WP_Query: Show 10 posts in date order, first three random
- How to remove only the latest sticky post from the loop