You can run multiple nested WP_Queries as long as you reset_postdata()
to get the outer loop back on track.
$query_args = array (
'posts_per_page' => '1',
'meta_key' => $count_today,
'orderby' => 'meta_value_num',
);
$outer_query = new WP_Query( $query_args );
while( $outer_query->have_posts() ) :
// conditional if
if( true ) {
// change the query arg in the inner loop
$query_args[ 'meta_key' ] = 'something_else';
}
// start an inner query with adjusted params
$inner_query = new WP_Query( $query_args );
// nested loop
while( $inner_query->have_posts() ) :
// ...
endwhile;
// After looping through a nested query, this function restores the $post global to the current post in this query.
$outer_query->reset_postdata();
endwhile;
Related Posts:
- How to limit the number of posts that WP_Query gets?
- Should we trust the post globals?
- Random sort within an already sorted query
- how to get a different html for odd/even posts?
- How to know if get_posts() failed?
- Using $wpdb to query posts with meta value containing current post_id
- Query posts from current year
- WordPress Number of Posts Not Changing With posts_per_page
- I have over 4000 posts, will querying some of them cause performance issues?
- Duplicate posts
- Looping through posts per category gives same posts for each category
- How to show posts rank based on custom field value
- How do I create Comma Separated list of attached image ids?
- How to add posts to wp_query result?
- Trying to put an array into ‘post__in’ => array() query not working
- Order post by year DESC and month ASC
- Should ‘setup_postdata()’ be reset with ‘wp_reset_postdata()’?
- How can I display a specific user’s first published post?
- Query All users that has post
- wp_enqueue_media() slows down my WP site
- How can I get all posts data from within a paginated search result?
- Search query – exact post title match
- Sorting posts alphabetical that have single digits
- Query posts from different categories in multisite
- Check if post exists
- posts_per_page doesnt work
- Sorting posts according to view counts not working
- Display page content AFTER a loop of posts
- Only display posts after current date
- Get posts by multiple ID’s (query)
- How to start with post number x?
- Exclude posts with empty post_content in wp_query
- Only display a certain number of posts, dependent on how many posts there are available in a query
- Improving WP_Query for performance when random posts are ordered
- Should $found_posts be returned as string in ‘found_posts’ filter hook?
- Query how many items to show in shortcode
- Get posts by name and taxonomy term
- WP_Query Authors OR Categories
- Get Posts that are in the current month or later
- Sticky post appears twice
- do_shortcode within post query
- How Can I Query a Specific Page From a MultiPage paginated Post
- Get Meta Key Value While Saving Post
- Most liked page not displaying posts
- Query get post,how to add comment box
- filter RSS feed in URL
- WordPress custom Query for Posts in Category display posts multiple times
- get query() without post content?
- How can I setup a relationship using categories in WordPress?
- Exclude the first ‘n’ number of posts of a tag from home page?
- Need to display a Jan 1st post as the site’s front page on Jan 1st, and Jan 2nd post as front page on Jan 2nd etc
- I have two post types in the same query, how can I use CSS to select only one post type?
- filter posts based on menu_order
- Bulk Post update_post_meta
- Count posts for pagination
- Show 5 posts and than 3 posts offset with pagination
- AJAX load more posts not using correct category and repeating the same few posts
- I need some explanation on global $post [duplicate]
- How to delete all posts with dead image urls?
- Numbering author posts in posts query
- How to run select query of post with category and tags?
- Querying posts from current category, using a variable as array argument
- Query posts from newest category
- Sort Events by Venue Title – Sort Post set by related post ids
- wp query custom orderby not custom field
- Query post for today, if no post get the previous one
- Custom Select Query pagination not working properly
- Query post by meta key of another table
- Sort / Filter Queries
- Query post category & remove any post id
- Display the latest post from some selected authors
- Advanced Post Display/Pagination/Ordering
- Ordering posts alphabetically by meta keys and title
- Why I obtain this WP_Query strange behavior trying to select posts having a specific tag?
- Why I have this strange behavior when I try to exlude the featured post from posts visualization?
- Grab next post title in archive page
- Removing a post from the results of a WP Query
- Query posts from a specific category and selected tag
- Continuous listing from a custom field
- Displaying Results From Custom Taxonomy Query
- Sort post by category using HTML Select tag
- How to store post rating system data post independent?
- Get first comment link on the post itself
- WP_Query retrieve custom posts but not the post showing
- Special Query: Title, Terms, Content – %LIKE%
- Creating pages and getting their values from the database table
- Make division in post_query?
- How to limit post query to only return a total count of items with certain post statuses?
- Display one post randomly from category on home page
- Wp_query WooCommerce products
- The page that displays Posts on homepage
- query_post or single-[cpt].php?
- Need to show birthday of custom post type(Famous people in this case) in elementor. Date of birth set in custom fields
- Display posts with id equal to relationship value
- Sort Posts Alphabetically Based on Specific Category (Divi)
- Change permalinks in posts via SQL
- I am using the query block and what I want and see on the editor page is not the same with the actual results
- Batch Scanning and Deleting Empty WordPress Posts
- Add a category to posts, that DO NOT contain two specific keywords?
- How to make WP_Query faster when getting a post from one year ago?