Initialize an array before the first loop:
$exclude = array();
Then add the ID of each post within the first loop to that array:
while ($query->have_posts()) {
$query->the_post();
$exclude[] = get_the_ID();
// etc...
Then use that array to exclude those IDs via post__not_in in the second query:
$args = array(
'post_type' => 'page',
'post__not_in' => $exclude,
// etc...
Related Posts:
- Inject post (from specific category) between posts in Loop
- Count number of published posts by type
- Get taxonomy terms only of the WP_Query current posts
- How can I use wp_query to show all product data using just the products ID?
- How to store post ID’s in cookie or session to display the same posts later
- WP_Query: getting posts where custom field exists
- Array to string conversion on array_map
- Store metakey value as an array
- Increment paged on WP_Query
- Modify main query before it run without pre_get_post
- Do not duplicate posts with multiple categories in multiple loops
- array_rand not working correctly?
- WP_Query() load selected post
- Wp Query : Order by distance lat,lon
- WordPress sorting posts by date and title using a dropdown
- posts_per_page displays only 2 posts instead of 4 posts
- Query on a repeater date (acf)
- Need to show 7 posts from actual date
- Check the database for a postmeta field
- Getting info about selected posts using one WP_Query
- How to display last whole post on the homepage
- Only Get A Certain Number of Posts From WP_Query
- insert thumbnail image from php script
- Weird Behaviour: Not all WordPress Posts appearing
- List of child pages fetch next results at link click
- Query category-specific, paginated posts and allow viewer to change sort order
- Why does WP_Query show only the same post even with different categories and endwhile?
- How to modify this function to exclude also the post belonging to a specific category?
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- get_the_content if it contains multiple lines it results in SyntaxError
- Looking to exclude blog posts from category Previous/Next buttons
- Automatic Excerpt Not Working
- Using a javascript file to access a get posts array
- WP_Query with the “offset” argument
- Display Post co-authored in Author Page
- Sorting Posts | Alphabetical Order
- Looking for most performant way to execute several similar WP queries within shortcodes
- Trying to make php run in a post
- Get the last month with posts using a recursive function…
- How to query for pages/post depending on slug?
- WordPress custom slug (endpoint) and compare all links
- wp_query with cat not working
- How do I “get the next 10 posts after post_id == x”?
- Recent Posts slider, using WP_Query(), loads duplicate sliders
- Why WP_Query(‘showposts=5’) shows only 1 post?
- Compare post-IDs within WP_Query? (Less than / Greater than)
- How to use $query->set with post__in?
- Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?
- Display a specific category of products in shop page and disable code for specific actions
- Which hook/action will help me solve my problem?
- get_posts() and WP_query limits ‘AND’ conditions to a maximum of 6 for meta value queries in WordPress
- Custom query, checking values of multiple meta keys
- Get children post mime type using parent post_ID wp_post
- unable to use ‘new WP_Query’ in AJAX call
- Pagination on Custom Post
- $wpdb returns duplicate posts
- Why is a wp function used in current PHP namespace’s callback not resolved to global scope?
- How to hide posts of a specific custom category in WordPress?
- Add new post in existing categories using wp_insert_post
- How do I add more options to the post-new.php page?
- Get the id of all images in a post
- Four posts per row [closed]
- Need help removing […] after excerpt
- displaying a fall back query if there’s nothing in the post-type category
- Pagination in single.php
- Isn’t Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}
- wp_query with multiple arguments with AND
- Nested Queries of decreasing specificity
- Wrong pagination results, page counter resets when navigating
- Is it possible to order posts using multiple meta queries, i.e. show posts from first meta query, then the second?
- in_array not working on dev server but works on localhost
- WP query posts BUT show the most recent and one per author
- Define global custom post
- How to get post image caption by index
- Is there anyway I can call the year once?
- Check $post->ID against multiple values
- Posts missing on author page
- How to allow users to save list of posts in wordpress?
- Search for single post by 2 tags
- How to center all text body in single.php at once?
- Woocommerce Related Product by 2 taxonomy
- Get current_post in reverse order with pagination
- How do I get the featured post to share on Social Media and show on Individual Post pages?
- Basic PHP question (displaying post title over thumbnail) [closed]
- Can’t get data from audio media
- WP_Query count of different meta key values [duplicate]
- Post from front end form to post_meta
- media_handle_sideload() returned post_id being incremented
- Saving Post redirects to 404 page when php $_GET variable in post body
- How can I add WordPress Audio Player as featured audio in Audio Blog Posts?
- Fix incorrect related posts code snippet
- How to customize posts in WordPress by using HTML and CSS?
- wp_query on search results page showing all results every time
- foreach all the post
- Bulk data save in custom table
- wp_logout action hook is not firing
- How to change post date and post time in WordPress automatically with a function
- Global update of records after import WP All Import Pro
- Add up all numbers from a WordPress query [closed]
- How to display only the first 2 words of a post title