As documented in the codex you could use the following
$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );
//post has to be in category with ID 2 AND 6
Related Posts:
- WP_query category__in not working, only pulls from first category
- How to get posts by category and by choosing a taxonomy term?
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- When should you use WP_Query vs query_posts() vs get_posts()?
- Using get_posts vs. WP_Query
- Delete all posts from WordPress except latest X posts
- How to query only for products with status “in stock” in WooCommerce? [closed]
- Sanitation needed for WP_Query or get_posts calls?
- Differences Between WP_Query() and get_posts() for Querying Posts?
- Use of caller_ get_ posts
- How-to exclude terms from the main query the most performant way?
- Query all posts where meta value is empty
- How to filter by category in REST API, excluding posts also in other category term?
- Meta Query with date and time on the same Day before given time
- Get posts with condition on comment meta value
- How to order category.php loop by ‘meta_value’?
- How can I display recent posts from a particular category in my header?
- Sorting Posts by custom field
- What should I use, get_posts or wp_query for less CPU load?
- Different Results with query(‘s=computer’) vs get_posts(‘s=computer’)?
- How to get post from all Blog Multisite to the Main Site?
- Count posts returned by get_posts in external PHP script
- WP Query with multiple categories – passing an array works?
- How get posts from a subcategory of a category by name?
- Get image of latest post from taxonomies/categories
- Adding multiple post queries with parent and children to page – Best Way
- How do you Query posts with nothing in common?
- WP_Query, tax_query and term_meta: How to?
- WP_Query arguments to fetch custom post type posts which are in certain category?
- How do I reset this wp_list_categories query?
- get_posts – get all posts by array of author
- How to get count of posts assigned to given category?
- How to show subcategories using loop?
- get_posts() not working with multiple statuses
- Remove a specific category ID from related post
- Complex Category selection as per user input
- Five posts from a category in footer
- Return only post(s) which have post_excerpt
- How to get all unique categories for posts in loop?
- Help with WP_Query – Too many mySQL queries
- Display Featured Post by Categories and Avoid duplicated posts
- category query for pages not working
- Query posts intersecting tags and categories
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- Check if loop has any categories?
- WP_Query post_parent parameter always returns children of current page
- Inserting HTML to close and open divs in WP_Query loops
- How to count posts with specific arguments
- Check return value of get_posts
- Query post with meta value in set of values
- Finding WordPress Posts assigned to multiple categories
- Combine results of multiple WP_Query to resemble single WP_Query
- get_posts output always same post
- WordPress Custom Search Form Displaying Unexpected Results
- get_posts() and global variables
- complex get_posts() query to select child pages
- wp_query is showing posts from other categories
- Posts query according to meta box date
- How to display a list of posts in same child category as current post
- How to exclude a category name from showing?
- Loop categories by recent post
- Function using get_posts() with tax_query not working when called from functions.php
- get_posts query matches too many results
- How to properly use Categories with Custom Post Types
- Search for pages with permalink
- How to query for a page, get data, then query for child pages of that page
- Widgets: Show Recent Posts Only if the Posts Have Both Categories X and Y
- Prioritising and Ordering Posts By Category Name Using A Custom Loop
- Related posts by current posts child category
- Filter products on category AND tag
- get_posts query is taking about 40 seconds to execute
- Populate select list with meta values from all posts of a Custom Post Type
- How can I have sticky posts while ALSO showing posts from a specific category using one WP_Query?
- How to use the Term Object from a custom select field in a query
- Related categories order posts by category
- How to get meta key list efficiently?
- Display All Top Child Categories / Taxonomy
- Query post by Category and custom file (ACF)
- how to get wp_query posts only first letter of alphabet A?
- Display 3 levels of categories on page
- Filtering ‘Featured’ posts from a batch of category IDs
- I cannot get tax_query in get_posts() to work with custom taxonomy
- Getting the last X posts, but in ascending order of time
- Help displaying related categories
- Show full category tree for a year with all post titles?
- query.php – multiple is_category functions
- Create multiple sections for all categories and then queries all the posts for each of those categories
- Why WP_Query in functions.php is not working when get_posts works?
- Retrieve latest post by multiple categories with ID
- Retrieve posts in custom post type and specific taxonomies
- Query specific number of posts for each post type in specific order
- Related posts with WP_Query
- query_vars category_name only display one catgory out of multiple categories
- How to replecate the _fields parameter in custom REST api endpoint
- WordPress extremely slow when using get_posts with multiple meta_query relations
- I need to get all categories from a WP_Query
- filter on get_posts efficiently
- Pagination only showed when no category is set in wp_query
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2
- Sort by multiple columns using get_posts