Use array_diff()
to remove the current post ID from an array of other post IDs.
$include = array( 17, 111, 108, 158);
$include = array_diff( $include, array( $post->ID ) );
$args = array( 'post__in' => $include );
$posts = new WP_Query( $args );
Edit to include a point raised by Pieter Goosen–If $include
is not a hard-coded array of IDs but something that comes from the database, you should verify that it is not empty()
prior to performing the query. Passing an empty array to 'post__in'
will get an unfiltered list of posts, rather than returning no posts as you might expect.
Related Posts:
- Exclude post ID from wp_query
- How to filter by category in REST API, excluding posts also in other category term?
- Get attached media only
- Query Ignoring ‘exclude’ Parameter?
- WP_Query Excluding pages with Order is 0
- Query: offset post list, unless it’s a specific category
- Excluding pages in WP_query using ACF
- Exclude Category filter from Portfolio section
- Avoid repeated post on page 2 when excluding one on page 1
- 2 wordpress loops showing 1 post from same post type – how to avoid showing the same post?
- Include posts from some categories while excluding from others
- Exclude recently updated post from custom WP_Query using multiple loops
- How to exclude Sticky from Recent Post?
- specific post is not excluded from the loop
- Paginate wp_query while utilizing post__not_in
- WP Query Obj: Set value to be unequal | Hide media by admin
- meta_query ‘compare’ => ‘IN’ not working
- Sorting: custom query with orderby meta_value_num THEN by title
- WP_Query and next_posts_link
- Sanitation needed for WP_Query or get_posts calls?
- count posts from custom taxonomy terms by year
- replace the WP_Query class in the main query
- Woocommerce query by price range and custom meta key
- Get posts with condition on comment meta value
- WP_Query sort by comment meta data
- Limit search results to exact match
- Move posts to top of WP_Query if in certain Taxonomy?
- WP_query ‘orderby=none’ Problem
- Sorting meta_value as integer doesn’t work
- Display different number of posts from one category on the different pages
- How to display child posts in the parent category
- WP_Query hit max joins… How else can I build a search function that uses custom fields?
- How to count data records in wordpress which have same meta_value in wp_postmeta table?
- Order terms inside a select dropdown
- Ordering by meta_value AND date NOT WORKING with wp_query
- Transient caching for wp query
- WP_Query orderby breaks when using AJAX?
- Query by custom dates in UNIX Time
- Custom WP Query from meta_value stored as serialised array
- Get List of all the Authors
- How to check if a post “does not have term” in conditional statement?
- Detect featured image among the attached images
- Pass arguments to WP_Query using “set” method
- Search Posts with Custom Fields as query
- WP_Query returns more results than expected
- Make WP_Query more efficient?
- Set order of returned items in the WP_Query() class/function
- Use query->set or make new query?
- Which method is faster to get a single post?
- Show Sticky Post at the top but do not show again in the loop?
- How to add posts, manually, to a wp_query?
- Use Repository Pattern in WP theme
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- WP_query with OR relation between args[‘s’] and arg[‘meta_query’]
- add_filter(‘query_vars’) not working in custom template
- Out Of memory issue on post per page parameter
- query_vars overriding WP_Query args
- How to count posts with specific arguments
- Why post__not_in is ignored?
- find posts that don’t have a custom taxonomy
- Exclude All Posts Which Do Not Have A Tag Assigned
- pre_get_posts – Trying to get property of non-object warning
- Replacing search results with custom external query
- get_posts output always same post
- WordPress Custom Search Form Displaying Unexpected Results
- Stomping WP_Query in author archive to facilitate pagination with custom queries
- Limit number of posts in loop
- How do I get Woocommerce product variation name and variation description in a WP_Query?
- WP_Query offset is returning post from prevois loop
- Does WP_Query ‘responds’ badly to empty arguments?
- Does putting queries within loops cause an issue on WordPress?
- I need to exclude from a query a category and a few custom taxonomies
- How to create Page templates for showing Blog posts in different layouts?
- How to get only ONE category of “Portfolio” posts to display on main page rather than ALL categories?
- Determining what post is displayed out of total posts found
- Adding query parameter to archive page returns 404
- Strange behaviour of hierarchical taxonomy archive
- Is it normal that get_post() gets run 300 times and the content of those posts aren’t visible?
- WordPress AJAX Request returns 400
- Using wpdb prepare with a variable, turns it into a big string
- WordPress Sub Category Archive, Show Extra Empty Page Number in Pagination
- SQL to join u3g_users & u3g_meta_value with repeating data
- How to get meta key list efficiently?
- Get posts having meta value between two numbers
- Separate by Category Post Type
- ACF: using two loops, the_field returns field content from another loop
- Using modified ‘meta_key’ to set ‘orderby’ in WP_Query?
- Combine WP_Query with array of custom data to single loop without breaking the pagination
- Query posts in current category but not attachment format
- WP_Query with Pagination and orderby meta_value_num showing duplicates
- wp_query – Modify $query to include duplicate content
- Optimising specific Query with ACF meta objects
- Slow getting posts from category
- Pagination not working with WP_Query (creates links but no page)
- WP Query posts__in not working with array
- Avoiding page loop
- Query Users by post count, last 30 days and display each users post count according to post type
- how do I exclude child categories from a wp_query?
- How to create a loop inside WP_Query?
- how to show only catergory titles on a page in wordpress