This touches upon an answer provided here Loop within a loop? except I posted the full code:
$query = new WP_Query($args);
while ($query->have_posts()) :
// initialization for $inner_args & backup the current global $post
global $post;
$backup = $post;
$inner_query = new WP_Query($inner_args);
while ($inner_query->have_posts()) :
// do something
endwhile;
$inner_query->reset_postdata();
// restore the global $post from the previously created backup
$post = $backup;
endwhile;
$query->reset_postdata();
The important thing here is you are setting the current post as a backup, because when the inner nested WP_Query
postdata is set up, the prior loop’s is lost. After the inner query finishes looping, you can set the post back to your backup and continue in the “parent” loop.
Related Posts:
- Using a custom WP_Query with get_template_part loop
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- How to make WP_Query ‘post__in’ accept an array?
- WP_QUERY Get posts by category and similar name (Like)
- Using get_posts vs. WP_Query
- Is it possible to orderby multiple meta_keys when using meta_value_num?
- Is it possible to wrap Geo Location search around WP_Query?
- How to Access Global $multipage or Global $numpages outside the loop?
- What is an efficient way to query based on post_meta?
- Merging a complex query with post_rewind and splitting posts into two columns
- Pagination wont work with search results template [duplicate]
- Pagination Not working on Home Page with 2 Query
- WP_Query in a shortcode
- ‘posts_where’ filter not applying ‘WP_Query’ in `wp_ajax`
- Get all posts without tags
- Filter wp_query to search post title in function
- Sort by meta key on archive page
- What are the differences between “Latest Posts” and “Static Page”?
- Retrieve or Query Pages by ID
- Searching through different categories on different pages code is not working
- Setup of taxonomy term template pages
- how to get the id for a post from within a post and return post meta for it
- WP_Query’s “request” SQL Query
- How to make this WP_Query run faster on a WordPress website?
- Order taxonomy terms in alphabetical order
- is_search was called incorrectly
- How do I sort posts with multiple pages
- Sorting By Custom Posts With Attachments
- Complex date range with WP_Query and BETWEEN
- Attachment changing page’s permalink – $post not resetting?
- List custom taxonomy terms sharing posts with a term from a second custom taxonomy
- Make (post) query a clickable link
- How to set pagination to work with ‘/page/’?
- How to do meta_query for attachments?
- Most commented last 24h, week, month, year and all time – posts_where
- How can I filter by slug depending on language selection
- How to duplicate 5 posts out of WP_Query results?
- Ajax WP_Query reutrns no results on author.php
- WP_query sorting can’t sort danish letters (æ, ø, å)
- Dynamic User Id
- How to get list of posts from permalinks?
- How to change main query to only query posts from a single category? [duplicate]
- wp_query not working with post_type
- WP_Query posts with comments only
- how to use pre_gets_posts to exclude one queried ID from homepage loop
- Default permalink structure causing Notice: Undefined property: WP_Query::$post
- How to check the array values, what WP_Query has brought to me?
- How to Order a list of taxonomies? orderby?
- queried_object using pre_get_posts gets notices and warning
- post_parent don’t work and return 0 page
- date_query in pre_get_posts out of memory
- Combine 2 WP_queries into one (OR relation)
- WP_QUERY wrong ammount of posts
- How to add sort order to incremented and paginated category loop
- Get specific ACF key and value from all posts – no access to DB
- Query only displays one page_id
- How to exclude a category name from showing?
- How to set parameters for search loop?
- Get posts with no tags?
- get_posts query matches too many results
- Sort by custom field that is an array?
- Register Taxonomy – What is `query_var`?
- Get Post ID as a separate RSS feed item
- how to retrieve WP_Query without ordering by date [duplicate]
- Which is from this queries is more faster
- Start Query from 2nd Post without offset
- Related posts by current posts child category
- Trying to change category of wp_query
- Any quicker alternative for WP_Query “NOT IN”
- duplicated posts when using pagination
- Is it possible to give a classname to specific comments in the WordPress admin?
- How can I have sticky posts while ALSO showing posts from a specific category using one WP_Query?
- meta_query order by date present -> future then show null
- How to upload 3 attachments to current post?
- In a WordPress multisite configuration, how do I instruct WP_Query() to return posts from a sub-site?
- How to query for exact string in custom field?
- Multi line of $wpdb->query just run 1 time and end the loop right after
- WP_Query for posts that have postmeta assigned to a taxonomy
- Paginated Author.php not using Author.php template
- Help displaying related categories
- A very strange problem with search query
- In Product Category archives how to show Posts having same/similar prod_cat slug structure?
- Custom Theme Building & Permalinks
- Custom loop – Isolating post meta output depending on current query taxonomy terms
- wp_query – show pages that have parent defined
- previous_posts_link not working in WordPress pagination
- Eliminate typical pages on search page, turn it into ajax search with “infinite” sized single page
- Orderby Meta Value and Query from Meta Query
- WP Query to order posts by multiple meta fields
- Sort posts in dashboard using custom field; also include posts where field isn’t set
- Display First posts without the default featured image
- paginate_links() with Custom Taxonomy
- Parsing External Table Arguments
- Query posts by meta value and sort by another meta key
- Output ACF field dynamicaly within a taxonomy loop [closed]
- Why does WP_Query not search for two ‘meta_query’ keys separated with OR?
- How do I split a large query with a semi-expensive function included into multiple smaller queries
- Custom WP_query in Jnews theme returns thousands of posts
- Query All Posts: Either Display Most Recent or One with Particular ACF Value Chosen
- Display only posts with thumbnails