Another Date Query Question

Hey folks,

Returning to this question from before, I’ve got half the equation fixed but can’t figure out its flipside.

So, quick TL;DR — we wanted to display products with a ACF launch_date of today or later. With the query string builder, I was able to get that string firing perfect:

post_type=product&post_status=publish&orderby=meta_value&meta_key=launch_date&meta_query%5B0%5D%5Bkey%5D=launch_date&meta_query%5B0%5D%5Bcompare%5D=%3E%3D&meta_query%5B0%5D%5Bvalue%5D={{dc:global:date format="Ymd"}}&meta_query%5B0%5D%5Btype%5D=DATE&order=asc

My functions.php looks like this:

function get_query_string() { 
$today = date( 'Ymd' );
$query = array(
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'orderby'             => 'meta_value',
	'meta_key' 			  => 'launch_date',
	'meta_query' => array(
	     array(
	        'key'		=> 'launch_date',
	        'compare'	=> '>=',
	        'value'		=> $today,
			'type' 		=> 'DATE',
	    )
    ),
    'order'               => 'asc'
);
var_dump( http_build_query( $query ) );
 } add_action( 'wp_footer', 'get_query_string' ); 

Excellent.

But when I replace the compare value with ’ < ’ I should get all posts with launch_dates before Today… but my ouput is nothin’. (We want an archive of classes once offered.)

Here’s the query string:

post_type=product&post_status=publish&orderby=meta_value&meta_key=launch_date&meta_query%5B0%5D%5Bkey%5D=launch_date&meta_query%5B0%5D%5Bcompare%5D=%3C&meta_query%5B0%5D%5Bvalue%5D={{dc:global:date format="Ymd"}}&meta_query%5B0%5D%5Btype%5D=DATE&order=asc

Here’s the URL we’re working on: https://radiobootcamp.org/cornerstone/content/288

Credentials will be attached as a secure note

Hi @lukefinsaas,

Thanks for reaching out.
I have checked the layout thoroughly with both the Query String and found the issue you described here. I would suggest please copy your live site to a staging server so we could troubleshoot freely without breaking your live site.
And give us access in the secure note including:

– WordPress Site URL & Login URL
– WordPress Admin username/password

To create a secure note, click the key icon underneath any of your posts.

Thanks

Thanks @tristup!

Hmm. This client’s hosting doesn’t offer staging servers, so I’ll have to copy it back to another server of mine. I won’t have time today, but tomorrow I can get that setup.

But also — feel free to be a bit rough with it during the US’s night (i.e., the next 5-6 hours), if you want to try some functions.php stuff or whatever.

They’re in EST time zone so we can experiment during the night, as long as we get the site back to working order by 7am EST or so.

Hi @lukefinsaas,

We avoid modifying directly on the Live server, please let us know once your staging server is ready for investigation.

Thanks

Just keeping this topic open — copying the site to a staging server now…

Hi @lukefinsaas,

Please let us know once it is ready.

Thanks

Credentials attached to this post!

Hello @lukefinsaas,

It looks like your Query String is incorrect. I went back to check the query. I referred to this old thread:

I was able to come up with this query:

$meta_query[] = array(
    'key'     => 'launch_date',
    'value'   => date('Ymd'),
    'compare' => '<',
);

$query = array(
    'post_type'  => 'product',
    'meta_key'   => 'launch_date',
    'orderby'    => 'meta_value',
    'order'      => 'ASC',
    'meta_query' => $meta_query
);
var_dump( http_build_query( $query ) );

Using the @Kory’s solution from this article (Looper Query String: Featured Products), I get this query string:
post_type=product&meta_key=launch_date&orderby=meta_value&order=ASC&meta_query%5B0%5D%5Bkey%5D=launch_date&meta_query%5B0%5D%5Bvalue%5D=20220313&meta_query%5B0%5D%5Bcompare%5D=%3C

where the “20220313” is the date of today. And to get the current date, you will have to use the dynamic content date “ {{dc:global:date format="Ymd"}}

Therefore the final query string would be like this:
post_type=product&meta_key=launch_date&orderby=meta_value&order=ASC&meta_query%5B0%5D%5Bkey%5D=launch_date&meta_query%5B0%5D%5Bvalue%5D={{dc:global:date format="Ymd"}}&meta_query%5B0%5D%5Bcompare%5D=%3C

Kindly check your page now if the items displayed were indeed the only products which the launch dates were already have passed.

Best Regards.

1 Like

This worked! Appreciate your help.

Hi @lukefinsaas,

You’re welcome and we’re glad that my colleague was able to help you with your issue! If you have any other concerns or clarifications regarding our theme features, feel free to open up a new thread.

Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.