If I understand it properly,
You can use the post__in parameter to query for specific posts where the post ID is in that parameter:
$ids = [ 1, 2, 3, 4, 5 ];
$query = new WP_Query( array(
'post__in' => $ids,
// ... other args, if any.
) );
// Then loop through the posts.
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
the_title();
// ... your code here.
endwhile;
wp_reset_postdata();
}
Alternatively, use foreach with setup_postdata() without having to do the new WP_Query():
global $post;
$ids = [ 1, 2, 3, 4, 5 ];
foreach ( $ids as $id ) {
$post = get_post( $id );
setup_postdata( $post );
the_title();
// ... your code here.
}
wp_reset_postdata();
Related Posts:
- post__in – Placing content from a foreach loop inside of an array
- Problem with displaying HTML content after in_array [closed]
- Can’t access PHP array inside script localization from javascript
- Output meta into arrays
- Trying to get property of non-object in shortocde
- How can I access specific posts brought back by query_posts?
- How to create a multidimensional array with multiple loops
- How to get user ID’s from multiple usernames?
- Loop through an array inside a class using foreach [closed]
- CPT while loop not working
- display post tags on single.php inside loop
- Exclude posts based on an array
- Add 2 values to post__not_in
- Query posts and display all dates in repeater field in chronological order
- Can’t print out returned value
- WordPress loop: Display if posts exist
- How can I access ‘key’ and ‘value’ from an array in a for loop?
- How to get files from loop for zip
- if ( is_home() && ! is_front_page() )
- Why am I being limited to ten posts on a custom loop?
- Child Pages Loop
- Loop.php vs looping inside template file
- Insert/sticky specific post into Loop at specific location
- Style every four posts differently [duplicate]
- Return vs Echo Shortcode
- have_posts() return false but count says “3”
- next_posts_link not working in custom loop
- Ajax loop in sidebar to load post titles/categories
- How to sort posts in archive loop
- wordpress post loop in javascript
- How to iterate through database until it find a match
- Show top level comments ordered by number of replies
- Redirect Loop to Page, Can’t Find Origin, Can’t Remove [closed]
- wordpress change the loop order by dynamic value
- Post Format single-loop.php using get_template_part
- Display posts from multiple user roles
- A loop with all articles with certain text strings in tags
- Prevent Headers from being sent
- How do I get row padding in a post feed?
- How can I create a legend (Key) like the one in the footer on WordPress StackExchange
- It is possible to encounter horrible bugs if I place a main loop that uses `query_posts` right above the main bbPress loop?
- How to show “teaser” posts on blog / archive pages, otherwise full posts
- Make blog page show 10 on first page, 9 on every other page
- Alter query using pre_get_posts() if meta_key is present
- Next Posts Prev Posts for Standard post format only
- Two loops on different pages do the same thing, but only one page works
- Posts active and expired in different loops
- Genesis multiple category custom loop 404s after first page
- Shortcode in posts called by ajax is not working
- Pagination not working in category listing [duplicate]
- 404 Template customization | Want 10 recent Post on the 404.php error page apart from the error Notice
- For each loop on every word in post
- Display name of taxonomy once
- Use have_comments() for current post instead of last post in loop
- Displaying the first, second, and third posts from a category in separate slides
- WordPress Alphabetical Sort Loop Issue
- Call wp-blog-header.php header without performing a wp_query
- Display post formats differently on index.php (loop in function)
- How to output thumbnail twice in a loop
- Frontpage pagination by week
- Saving return value from the_author_posts_link()
- Display more than 10 posts on author.php file
- Accessing the Posts page Content
- how to run loop in function.php that sends email based on specific conditions?
- How to use this $tax_selection variable in this custom loop?
- Display all categories as plain text
- Forcing page to top of the list when using get_pages
- How to display fields from the loop in two separate divs
- WordPress posts loop not displaying all posts on blog
- get_page_by_title not working inside fucntions.php
- Duplicate posts on archive page
- Bootstrap tabs are not being clicked in WordPress loop
- Get gallery in loop through ajax
- Repeat array inside array through while loop
- Pagination on a underscore custom theme
- Build a Page which shows the Oldest Post with specific Custom Field Value
- WordPress multiple loops with default pagination
- Loop random posts and display odd and even based on two alternating meta values?
- Get top Page IDs from menu and cycle through their child pages on a scroller
- Style first 3 posts differently and use a 2nd loop to get rest of posts / offset and pagination broken
- Posts Per Page is Not Returning Correct Number
- Add specific class to featured posts
- Echo Most Recent Sticky Post in Loop?
- WordPress Tags in class
- Set loop format & have a loop inside other one
- The Loop isn’t working
- Custom loop page with post navigation
- Pages with a loop (index, archive) are loading the first image as post_thumbnail
- the loop – how to control whether wp or plug-in runs it
- stuck with template hierarchy
- How to create an identical second loop for attachments?
- Randomly Display One Image From a 10-Image For Loop
- How to show only specific tag in wordpress loop
- WooCommerce – Checkout suddenly stops working [closed]
- pull 500 post of many from database [duplicate]
- WordPress loop put title into variable
- How to add a continuous number to HTML tag attribute value inside The Loop [closed]
- WordPress Not Sorting By Custom Field
- Landing page with login
- How can I create an entirely new, separate display of posts?