WP_Query Date comparison with Text Field - wordpress

I am trying to retrieve only Posts that are not past an expiry date.
The field that holds the Expiry date is a text field.
When I use the code below it seems to compare as a numeric, specifically the first two digits.
I'm guessing the issue is that I am comparing Apples and Oranges (DATE v NUMERIC) - is there a coding solution rather than changing the DB Field to Date Type?
The Date is stored in the following format: 13-02-2023
Thanks
// Only Jobs that haven't expired
$meta_query[] = array(
'key' => '_field_dateexpires',
'value' => date('d/m/Y'),
'compare' => '<=',
'type' => 'DATE'
);

Related

Wordpress: WP_Query ordering anomaly

I'm ordering a set of posts by an ACF date field using these arguments in WP_Query
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'date', //ACF date field
Which seems to work, except it's throwing an anomaly.
This is an echo of the list of dates (stored as Ymd)
20211221
20220118
20220125
20220201
20220208
20220215
20220222
20220301
...
20221122
20221129
20221206
20221213
20221220
20220111
The last one, which should be second, is appearing last. (they are weekly dates starting 21st December 2021
Try to use meta_value_date instead of meta_value as orderby parameter.
see also https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

Using wordpress and advanced custom fields, how can I use a subfield for my meta query?

I'm using Wordpress with the Advanced Custom Fields Pro plugin and I'm trying to get all the posts that have a date field that is after today's date. I looked at this example https://www.advancedcustomfields.com/resources/date-picker/ and that works fine except that my field is a subfield of a repeater field. And I have some trouble making a meta query for that. I tried using _%_ for the subfield but then I read here https://support.advancedcustomfields.com/forums/topic/meta_query-for-a-group-field-sub-field/ that I can just use an underscore for the subfield. But posts still aren't coming up with any results. I tried checking the date from just a single post and it appears to be working correctly. I also read here advancedcustomfields.com/resources/date-picker that the date is always stored as Ymd in the database. I tried doing it with another field that isn't a subfield and that worked, so the problem lies with the subfield I think 'key' => 'date_startdate',
I am using the date picker as a subfield.
function do_get_upcoming($atts = [])
{
$defaultLimit = 8;
$limit = $atts["limit"] != null ? $atts["limit"] : $defaultLimit;
$today = date('Ymd');
$posts = get_posts(array(
'numberposts' => $limit,
'post_type' => 'events',
'meta_query' => array(
array(
'key' => 'date_startdate',
'value' => $today,
‘type’ => ‘DATE’
'compare' => '>',
)
),
));
echo count($posts);
}
add_shortcode('get_upcoming','do_get_upcoming');
screenshot subfield
I found the solution. Subfields can be accessed by adding _0_ so you'll get 'key' => 'date_0_startdate'

ACF compare dates from wordpress query

I have an ACF custom field with text as type but it stores date (string format), I have to search all custom post type between two dates and I don't know how to handle this.
$query = array(
'key' => 'registration_date',
'value' => array($startDate, $endDate),
'compare' => 'BETWEEN',
'type' => 'date'
);
Is not working because custom field value is a string format and comparison is not possible.
How can i search post type between $startDate and $endDate then?
Thanks for your help
I noticed that date comparaison only works with format YYYYMMDD, if you use something else like "DD.MM.YYYY" this will not work.
Your options are :
1. storing YYYYMMDD
2. using custom mysql queries.

Wordpress get_next_post based on custom date field

I have a Wordpress news site.
The news stories are added as posts.
To have more control I have a custom date field on the posts.
The posts are displayed in date order using the date in the custom field.
So the order the posts are displayed on the site can sometimes be different from the order of
the posts in the WP backend which are in the date they were added order.
On the single.php page I have a next and prev post links.
The next and prev post links use the order of the posts in WP which can be different from that on the front end.
Can I use the WP next,prev function with a custom date field instead of the actual WP order of posts. So the next and prev links will link to the next and prev links shown in the front end of the site.
I am not sure there is a solution using get_next_post(), but I guess you could query a single post with a higher/lower date using your custom meta.
Something like this should work, but I did not test it.
$args = array(
'meta_query'=> array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $currentPostDate,
'type' => 'DATE',
)
),
'posts_per_page' => 1
);
query_posts( $args );
Keep in mind this note from the documentation:
type (string) - Custom field type. Possible values are 'NUMERIC',
'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME',
'UNSIGNED'.
Default value is 'CHAR'. The 'type' DATE works with the
'compare' value BETWEEN only if the date is stored at the format
YYYY-MM-DD and tested with this format.

WP_Query filter by value of Custom Post Type

I am unable to filter results by a custom post type
This is my while loop which returns all items. I need to filter by a date, only showing date earlier than CURDATE()
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$auction_query = new WP_Query();
$auction_query->query('post_type=auctions&orderby=meta_value&meta_key=auction_details&paged='.$paged.'&posts_per_page=20');
in my wp_postmeta table I have the following data
meta_id: [id of meta row]
post_id: [id of post]
meta_key: auction_details
meta_value: a:6:{s:12:"auction_date";s:10:"2013-07-27";s:12:"auction_time";s:21:"9am Personal Property";s:16:"auction_location";s:44:"New Providence,";s:0:""
I need to filter by the auction date value in the meta value column when paired with the meta_key auction_details showing only posts with auction_date earlier than today's date.
It does not make sense to use CURDATE because the MySQL field type used to store post_meta values is a string, and not a date.
Consider storing your time and date values
in a separate post meta field, and
as a unix timestamp (number value).
Then it will be easy to restrict your query with a > or < comparison, something like
$args = array(
'meta_query' => array(
array(
'key' => 'auction_time',
'value' => date_i18n( 'U' ),
'compare' => '<',
)
)
);
$auction_query = new WP_Query( $args );
If you'd like to order by this field, be sure to order by 'meta_value_num' instead of the usual 'meta_value', as the latter will order your posts lexically and not numerically.
(A word of caution: Be careful to use date_i18n() (a function provided by WordPress using your WordPress timezone preference) rather than date() (a native PHP function using your system wide timezone setting in PHP) when converting timestamps while using WordPress.)

Resources