You’re just about there. Each date_query
array represents a separate condition to filter by. By default, these are combined using AND
which limits the scope further. In your case, you want to change the relation to OR
and then add the different years as their own separate conditions in the array.
$args = array(
'date_query' => array(
'relation' => 'OR',
array(
'year' => '2018',
),
array(
'year' => '2015',
),
),
);
The resulting mysql statement looks like this:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND ( YEAR( wp_posts.post_date ) = 2018 OR YEAR( wp_posts.post_date ) = 2015 ) AND wp_posts.post_type="post" AND (wp_posts.post_status="publish") ORDER BY wp_posts.post_date DESC LIMIT 0, 10
The important bit is ( YEAR( wp_posts.post_date ) = 2018 OR YEAR( wp_posts.post_date ) = 2015 )
Related Posts:
- Posts with at least 3 tags of a list of tags
- Get post count of current loop when using multiple queries on one page
- Query Posts in a Predefined Order
- Use WP_Query with a custom SQL query
- Display Posts by modifying the where clause only for my query
- page was loaded over HTTPS, but requested an insecure image
- WP_Query: How do I sort on meta value and use LEFT JOIN?
- Pagination causes error 404 when used with front-page.php
- How to pass custom parameter to WP_Query for filtering in pre_get_posts
- WooCommerce: filter by parent product’s taxonomy and product variation’s meta data
- How to search by title or tags exclusively?
- Using AJAX to return search form results
- How to get a category in a list item class
- Add custom search results to main WP search [duplicate]
- What is the difference between RELATION “AND” and “OR” in TAX_QUERY?
- Should close $wpdb via $wpdb->close()
- Finding post content that begins with a specific character
- Query Set Order By Author
- ‘&’ causes an error in my shortcode when I list the content of the page
- SQL Query inside Widget
- WP_Query with multiple meta fields filter?
- Use ‘parse_query’ filter to show posts that from multiple criteria
- How can I pick a single post from the latest 3?
- Get posts for last working week in WP_Query
- How to select events within current week using wp_query
- how to link to detail page(single.php?) in a wp_loop
- WP_User_Query unable to retreive all user at once
- Meta Query Array Error 500
- Display posts from only one post form in custom query and exclude in main query
- Get pagination working for custom loops within page templates
- Complex WP SQL Query
- Hide Administrators From User List except current user (administrator)
- Custom Query num_rows returns wrong amount
- Custom posts visible in admin, but truncate in public
- How to figure out redirection and contents in hybrid solution containing word press content and legacy code (ASP.net)
- Category applied to pages, creates multiple breadcrumb entries after a search query (On the translated site)
- Most commented post showing 2 records
- Query to get siblings and parent page
- Why does get_posts only show results for Admins or logged-out users?
- How to store and receive variables in WP sessions?
- How to list posts with disabled/closed comments, with pagination?
- Category ‘pad_counts’ & ‘parent’ conflict
- how would i change post->ID to work correctly when querying pages?
- WP User Query with Custom Fields and Search Results
- Event with multiple dates, display events chronologically
- How to exclude Sticky from Recent Post?
- Display if author page is author page of current user
- WP_Query order by not working properly due to category I believe
- get_query_var always returns the default value
- Modify WP_Query using pre_get_posts but only for frontend query?
- WP_Query to get post on frontpage
- When listing child pages run out of memory
- Function to retrieve IDs of posts, cache results, and improve wp_query
- parse_tax_query causing Navigation Menu To Disappear
- the_post_thumbnail do 2 queries. How to optimize
- Search.php – return number of results but cannot loop through
- Creating ‘posts page’ loop based on the page itself
- How to get pages of parent (non-recursive)?
- Can’t get term id for category archive
- WP_Query Orderby meta_key and hide some meta_key
- Show X taxonomies of the latest published posts
- Combine query in WP_User_Query()
- Nearby locations using Advanced custom fields, maps?
- Custom wp_query inside a conditional stament inside a template part doesn’t work: why?
- create custom shortcode wp and put php code in
- WordPress Query custom ordering by temporary variable
- Check the stored / cached WP_Query with transients on post change
- New template file does not load category-specific post
- How to create better WP_Query to look for date time which is anywhere between two meta values?
- Load WP Query with Ajax
- How to get_comments() ordered by date and parent?
- WPQuery loop not giving expected output
- WP Query filters active on wrong query
- Not able to fetch woocommerce variation sales products for particular category products
- Global page ID variable empty error
- WordPress tax_query ignoring relation OR
- pagination functions are not working
- Gather same custom field values in one value in a select tag with wp_query
- Fetch only categorized posts
- Wrap group of wp query posts to parent div by date/year
- Continue loop after $queryObject
- Order Posts By Youtube Views
- WP Query with sticky posts and tax_query
- Wrote a WP Cron Plugin and it triggers a fatal error upon activation
- Declare inline background image in functions.php
- WP_Query for attachments without duplicating post_parent and displaying tagged image
- Get the type of an advanced custom field (ACF) in a WP_Query loop [closed]
- Getting Year & Date inside wpdb [duplicate]
- loop through custom post types with meta data
- Looking for a way to exclude frontpage and nav menu from query filter
- Using Query In Post Type Archives
- Query posts by custom fields
- WP Query Obj: Set value to be unequal | Hide media by admin
- WordPress pagination returns the same posts
- Get posts by meta value except one post [closed]
- Using ‘meta_query’ with the ‘pre_get_posts()’ hook disables searching for post titles
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Get posts using multiple values from ACF checkbox as meta query wordpress
- How to save the results of a query as a php file for an autocomplete search bar
- Why ignore_sticky_posts argument is in sticky post query?