Skip Navigation

[Resolved] cannot query custom field using date between

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Christopher Amirian 1 year, 6 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2610715
Screenshot 2023-05-27 at 6.44.22 PM.png

Dear Sir/Madam,

Refer to below code, why it always return 0? Custom field wpcf-termination-date is date field

	$args = array(
		'post_type' => 'member',
		'meta_query' => array(
			array(
				'key' => 'wpcf-termination-date',
				'value'	=> array('2023-05-20', '2023-05-27'),
				'compare' => 'BETWEEN',
				'type' => 'DATE'
			),
		),
	);
	
	$query = new WP_Query($args);
	printf("%s<br>", $query->post_count);

If I do query with below code, it returns 6 records

	$args = array(
		'post_type' => 'member',
		'meta_query' => array(
			array(
				'key' => 'wpcf-termination-date',
				'value' => '',
				'compare' => '!='
			),
		),
	);
#2610873

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Toolset stores the dates in Timestamp format. That is why you need to use NUMBER for compare and turn the dates to timestamp:

https://wordpress.stackexchange.com/questions/283867/comparing-timestamps-in-meta-query-doesnt-work

hidden link

Thanks.