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
- Retrieve each widget separately from a sidebar
- how to upload image using wp_handle_upload
- Avoiding using a loop to access a single post
- Adding ‘current_post_item’ class to current post in the loop
- WordPress Alphabetical Glossary close div in loop
- Where should I use post_class()?
- Listing all posts from current category on page
- Landing Page – Redirect Loop?
- How to make multiple Column in archive Page?
- Sort Popular Posts by Views for the Last Week
- get_the_foo() in the loop – does it perform another query?
- Filtering posts by multiple taxonomies
- Is it possible to display previous post revision?
- Check if date of post is yesterday
- Fix inefficient loop breaks post.php on form submit
- Adding shortcode closing tag after a loop
- Get Pagination (WP-PageNavi) not to work
- Filtering The Loop For Single Page and Blog Page
- the_content() seems to block my ACF-code
- Pulling current post/page data into header.php
- Removing the_content() from the page
- Loop and Page template : my WP_query don’t take args
- Change front-page layout after x amount of posts ( while in the same loop)
- Filter the loop by categories using checkbox form
- Get single post from tags array
- Duplicated posts on category page
- WordPress loop: exclude if it is the latest post
- Apply an Incremental Counter in an Array Function
- Get all posts including sticky ones with get_posts(),setup_postdata(), and foreach loop?
- Checking array against author id in loop
- query_posts() vs get_posts() multiple loops [duplicate]
- Page-loop is looping content from custom loops on the same site. How to avoid that?
- How do I make a variable available inside partials?
- WordPress: Loop Help
- Woocommerce: How to display product price [closed]
- Unable to change the “Read more” text inside the_excerpt
- Display Post Author Link above the loop
- Enabling users to control custom loop’s query
- How to get the excerpt for is_home() outside of the loop isn’t working
- Category links including all posts
- Custom loop ordering not working
- First archive page with a few posts
- Hiding a row in the loop if empty
- Loop through ACF taxonomies and output associated posts
- get_delete_post_link() inside Loop stubbornly returns nothing
- Query posts only with actual text content (not including shortcode or images)
- Get markup for post without loading into the template
- How can this multiple loop have pagination as described?
- Default featured image set as background image [closed]
- Breaking up the results of a loop
- How do i create a list-posts-page?
- Is file_exists() compatible with timthumb.php? [closed]
- List ALL posts by author on author archive
- Using is_page_template inside loop
- Set depth of pages to fetch
- Trying to get all links in my posts
- Creating a User-Adjustable chart in WordPress
- Archive: Lists itself
- Staggering featured post using ‘sticky’
- How to show the last article in a different way in my blog?
- Reorder posts in a loop: have the posts by one particular author below the others
- How to execute a user loop with shortcode
- how to get author comment inside the loop?
- How to create loop in custom page, and get id from url into this loop?
- How to fix pagination for custom loops?
- Check to see if a field is within an array in twig
- Archive sorted by month – separate
- Display single post inside accordion based on form submission results
- One page loop issue with posts
- Humanmade | Custom meta box class: How to display a repeatable meta box group?
- Building theme so user can change header image [closed]
- Get query result before posts are displayed?
- WordPress Group By Problem
- display only parent replies count in index.php
- using the loop on a page
- Home page loop with pagination problem
- get_page() unlike Loop returns the post content without html tags. How can I fix this?
- Problem with ms-thumb-frame-selected class in Master Slider
- Fix html inside a for loop [closed]
- Display all posts in a page code for template
- How can you make it so the comment box shows on some pages or posts but not all?
- Divide loop into several columns based on post custom field and enable infinite scrolling