For multiple post IDs, in WP_Query
arguments, we can use:
'post__in' => [ 272, 282, 292 ],
but for a single post ID it’s:
'p' => 123,
Note that the default post type is post
.
Checking out the SQL query
It’s informative to checkout the generated SQL query of WP_Query
.
Try e.g. in the wp-cli shell:
wp> $q = new WP_Query( [ 'post__in' => [ 272, 282, 292 ] ] );
wp> echo $q->request;
That way you can make sure it’s generating what you need.
Compare e.g. the post__in
and the p
case.
Helpful docs
It’s also helpful to look at the Developer’s Handbook:
https://developer.wordpress.org/reference/classes/wp_query/
and in some cases the old Codex:
Related Posts:
- order by numeric value for meta value
- numberposts? showposts? posts_per_page?
- $GLOBALS[‘wp_the_query’] vs global $wp_query
- Some doubts about how the main query and the custom query works in this custom theme?
- Set Alias for meta_query arguments in get_posts()
- How can I use order_by to order by two meta_keys without excluding posts that don’t have those keys initialized?
- Loop within a loop?
- How to query only for products with status “in stock” in WooCommerce? [closed]
- WP_Query displaying ALL posts
- Does WordPress sanitize arguments to WP_Query?
- How to get posts in different language from WPML Plugin [closed]
- WP_Query meta_query where meta value ends in space
- Highlighting Sub topic in a post?
- wp_query for the first sticky, then display the rest of the posts excluding the first sticky
- How to combine tax_query and date_query in WordPress
- How to get any tag ID
- WP_Query with many meta_query arguments taking a long time to load
- Slow speed and high CPU usage for my code. How to improve it?
- Multiple queries on Home Page – page load/site speed issue
- Targeting specific pages in the loop
- WordPress custom archive page
- query attachments of parent page if attachments of current page are smaller than …
- Show all parents and children in custom post type in right order
- Show most popular post of last 12 months
- Refine/Filter WP_Query Posts with jQuery or Ajax
- Can’t get drafts with WP_Query using post_status parameter
- Get ONLY the count from wp_query without fetching posts
- Exclude admin from WP_Query Contributors
- Order by custom table and multiplication
- Storing query variable and then using wp_reset_query
- WP_Query posts by distance based on LAT & LNG in Database
- exclude posts with a specific custom field and value
- Redirect page when URL parameter not set
- list pages using page.php and NOT page-{slug}.php
- Changing WP_Query params with url Query Var
- Moving the array pointer inside a WP_Query while loop
- From where is archive.php getting its posts?
- Help with WP_Query – Too many mySQL queries
- Query posts(CPT, pages , hierarchical) by Ancestor ID
- How can I make WP_Query get posts that have a tag but also have random other tags?
- Get a list of posts with associated meta_value
- Access WordPress query by index number outside the loop
- Pagination outside of loop
- Multisite – Global CPT – advices?
- Combine two queries into one to use in loop
- database query with more than a couple meta hangs and doesn’t complete
- Gallery shortcode numerical sorting
- Check return value of get_posts
- Query post with meta value in set of values
- get_template_part causes 500 error
- Finding WordPress Posts assigned to multiple categories
- The most effective way to split by post type in custom query?
- Does meta_value (array) work with ‘orderby’?
- Get a list of posts by specific category
- WordPress Query String: get_posts(‘cat=5’) vs WP_Query( ‘cat=5’) vs URL: /site/?cat=5
- WP_Query – Adding “offset” posts to the end of the loop
- Shortcode for custom query not returning results when attributes are added
- wp_query is showing posts from other categories
- Multiple wordpress queries with nested output (odd – even)
- Get posts in taxonomy randomly
- Page navigation doesn’t seem to work when I add offset?
- it’s possible make a WP_Query with math operations?
- show posts with range meta key
- Using a Shortcode to List Posts from Multiple Categories
- is_404() – where does this get set?
- wp_query on page template not working (Genesis framework)
- Max Posts and Memory Limit
- WordPress | WP_Query does not return anything with s=a
- WP_Query Pagination
- WP_Query orderby
- Using Ajax and WP_Query to load more posts on category page fails
- I have problems with the search query using multiple post types
- Where my home page and font options has gone?
- Different query result on local and server
- meta_query compare > not working
- 404 page on custom search pagination
- WP_Query about meta_key and its array value
- Query string order by custom field
- Paging works correctly on local version, but not live?
- Create global array to compare inside a query
- Combine Tax Archive and Meta_Query in WP_Query
- Get posts by birthday
- How to add in WP_Query to every 3 posts displayed? [duplicate]
- Paginate nested query of child posts
- Hi , i am trying to set the post for 24 hours and with it will be changed
- Own query vars are not displayed
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- Show full category tree for a year with all post titles?
- Get Current CPT taxonomy category in shortcode
- Display images with same specific value?
- Custom Content Slider stop duplicate
- Sort Posts with custom meta key by default which is currently set as optional
- wp_query pagination links producing 404
- How return normal search result using wp_ajax action for wordpress
- Custom post types loop on a page template
- Pagination problem with multiple loops on the same page
- how to use transient method?
- WP_Query – meta fields numeric
- How to query posts from single post format on Genesis framework
- Display Featured image from custom post type category (custom taxonomy) wise