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:
- No exposure on search engines
- Related posts with WP_Query
- Adding css tweak based on page template
- Renaming or Moving WP-Content Pros/Cons
- Carousel slider with WP_Query to show 3 posts on each slide
- how to use transient method?
- Displaying specific posts from query thumbnail outside div
- Query posts only shows 1
- Get users query makes the site loading time too big?
- how do i remove plusone js from WP? [closed]
- Fatal error with wp-livechat plugin
- WordPress query very slow on +/- 300k DB entries and 7 INNER JOIN
- How to create a loop inside WP_Query?
- Add banner after the third post [closed]
- Get author meta data with no published posts in author.php?
- Query for page content, and query for posts on the same page?
- Can’t seem to get an else statement correct? [closed]
- WP Query Obj: Set value to be unequal | Hide media by admin
- How to sort by post_date and meta_value_num?
- 294 Queries on Mainpage of WordPress
- WordPress pagination returns the same posts
- get_posts shows current post, not defined posts with args
- Weird query with get_posts and WP_Query
- Adding arguments to WooCommerce Product Loop using AJAX
- How to hook the pre_get_posts filter via ajax call
- WP_Query and DES sort for Custom Taxonomy based upon a meta field?
- Help with showing text when something is entered in my search bar
- Remove divs and spans from post content
- Get Related Posts by Tag if Tag is Empty Get Posts by Category
- How to write a query-function as a query-shortcode?
- orderby and order filter in get_posts or WP_query function in wordpress not working
- How to show featured post first, then separate loop for other posts
- How to order “top rated” results by date and meta key?
- error with $query->have_posts()
- Output ACF field dynamicaly within a taxonomy loop [closed]
- Main loop querying current template’s info only in custom category archive pages, not my posts
- Multiple Orderby is not working right
- Why does WP_Query not search for two ‘meta_query’ keys separated with OR?
- Getting posts via WP Query
- Print post data inside loop using theme content template
- shortcode with $atts with strange results
- Show linked products using wp query and woocommerce
- Where to find WP_Query class?
- Standard Loop – wp_query
- tax_query not working properly with get_posts
- Why wp_query return 1 row if i have 3 row pass in it
- Query custom post types by meta field in a term from custom taxonomy
- WP_Query with tax_query not working
- Edit WordPress loop, taxonomies, & filter
- Pagination with Custom Post Type not working on index.php: I get a 404
- how to add limit records in wordpress query
- query only direct child and sub-terms of a current term archive
- restrict posts per page
- WP_Query – meta fields numeric
- First post outside of loop, homepage only?
- How to query posts from single post format on Genesis framework
- How to write short code with if else to get page_id?
- the_excerpt() not working in custom archive
- Filter posts by category
- how to show only catergory titles on a page in wordpress
- Limit the number of posts via wpquery
- How can I filter a query by post id?
- Get posts by meta value except one post [closed]
- query_vars category_name only display one catgory out of multiple categories
- Incorrect posts displayed on category page
- Pagination on custom post type not working
- How can I echo out the user id in user meta?
- Can’t get LIKE to work with wp_query [closed]
- Get all user with both meta_value
- have_posts order by title descending
- How to handle wp_query?
- Hook to get the query result after listing posts
- Undefined variable [closed]
- How can I display sticky posts at first in wp_query?
- How do I split a large query with a semi-expensive function included into multiple smaller queries
- Elementor custom query
- Why does $post_id = $wp_query->get_queried_object_id(); return 0?
- Custom WP_query in Jnews theme returns thousands of posts
- WP Query – if there are no posts with certain tag query other ones
- How to know which editor published a post programmatically?
- How does ‘category__not_in’ differ to cat => ‘-1’?
- wp_query loop with compare operator simply not working, why?
- Custom Wp_query loop takes very long
- Get access to all terms associated to each post that the wp_query loop displays
- Pre_get_post on CPT archive page
- WP query based on two meta queries
- How to account for empty key values in wp_query?
- Get comment number by date range?
- WP_Query – order with usort by custom meta
- WordPress website links redirect to homepage
- Filtering product search results using tags
- WordPress Show 4 posts from 4 specific categories in a slider
- How to display posts from a specific term first then another posts from another terms using tax_query
- WP_Query by meta key not returning any posts
- How to replecate the _fields parameter in custom REST api endpoint
- Get posts that were most recently tagged
- How to retrieve _embed (etc) fields in get_posts custom query?
- how to fetch post_name from WP_Query using Group by a custom value field
- Using ‘meta_query’ with the ‘pre_get_posts()’ hook disables searching for post titles
- WordPress extremely slow when using get_posts with multiple meta_query relations