Date Query
Ajax Load More supports WP_Date_Query parameters for displaying posts by post date, date before/after, and date range.
Introduction
WP_Date_Query
is designed to help developers create date-based queries for WordPress, allowing them to filter posts, pages, or other content types based on date ranges, specific dates, or other date-related criteria.
Custom date queries can be implemented for an Ajax Load More instance by utilizing the date_query
, date_query_before
or date_query_after
parameters.
For example, the shortcode below implements a custom WP_Date_Query
to display posts published more than 1 month ago.
Example Shortcode[ajax_load_more date_query_before="-1 month"]
Parameters
The following parameters are available to implement a custom date query with Ajax Load More.
date_query | Query with a custom WP_Date_Query. Accepts a formatted date string – YYYY-MM-DD-HH-MM-SS. |
---|---|
date_query_compare | The operator for how multiple dates will be compared. Accepted values: ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘IN’, ‘NOT IN’ See WP_Date_Query::get_compare(). |
date_query_relation | How the sub-date queries should be compared. Default = AND |
date_query_column | The DB column to query against. Default = post_date |
— | — |
date_query_before | Date to retrieve posts before. Accepts a strtotime() compatible string. |
date_query_after | Date to retrieve posts after. Accepts a strtotime() compatible string. |
date_query_inclusive | For after/before, whether an exact value should be matched or not. |
Attempting to query by an invalid date value (e.g. month=13) will generate SQL that returns no results. In these cases, a _doing_it_wrong() error notice will also be thrown.
Standard Date Query
The Ajax Load More date_query
parameter accepts a formatted date string of the following dash-separated options: YYYY-MM-DD-HH-MM-SS
.
date_query
parameters are automatically parsed into year, month, day, hour, minute, second, and week based on the values passed to Ajax Load More.
At a minimum, a year value (YYYY
) is required for creating a valid date query.
Note: Separate multiple dates with semi-colons. e.g. date_query="2022;2024"
Basic Date Query Examples
Query posts by specific year (2022
).
[ajax_load_more date_query="2022"]
Query posts by exact post date.
[ajax_load_more date_query="2024-12-25"]
Query posts from multiple dates.
[ajax_load_more date_query="2025-01-21;2023-11-08" date_query_relation="OR"]
Date Query Between
Query posts between two dates.
[ajax_load_more date_query="2023-01-01;2025-01-01" date_query_compare=">=;lessthanequalto" date_query_relation="OR"]
Note: When using date_query_compare
parameters that contain <
you should consider converting these to the helper strings provided by Ajax Load More as they may parsed as HTML elements during the render.<
= lessthan
& <=
= lessthanequalto
Before & After Date Query
The date_query_before
and date_query_after
parameters accept any strtotime() compatible and formatted string. These Ajax Load More parameters relate directly to the WP_Query before
and after
date parameters.
Note: Before or After parameters can NOT be used in combination with other Date Query parameters.
Date Before
Query posts older than 2 years from today’s date.
[ajax_load_more date_query_before="-2 years"]
Date After
Query posts from the past week (7 days).
[ajax_load_more date_query_after="-1 week"]
Date Between
Query posts between two dates. For example, displaying posts between 2021
and 2025
.
[ajax_load_more date_query_after="2021-01-01" date_query_before="2025-01-01"]