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.
Related
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'
);
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'
Looking for a point in the right direction, i've been working on an events plugin for a site, quite a simple plugin, adds a custom post type called events, adds some custom taxonomies, used ACF to add the fields to the CPT.
But I need to add 'start date and time', and 'end date and time' fields to it in a way that I'll be able to sort by start date/time in the admin side, and be able to sort by them when I query the posts as well.
I thought I might be able to do this with the ACF date/time field, but it's just not playing ball. Any ideas?
If i'm understanding correctly, you want to sort your results by a meta value right? that meta value being the date/time field. In your loop, you may want to try out something along the lines of:
$your_custom_query = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'date_time_meta_key_here',
'compare' => 'EXISTS',
)
),
'orderby' => 'date_clause'
));
See the original wordpress documentation here for more info: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
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.
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.)