I have a client who publishes events on his website. I have a Query that is supposed to display all scheduled events, meaning the date of the event is in the future.
For the date of an event I use a date ACF.
I don’t want to delete old events immediately to avoid 404 hits but events from the past should of cause not be listed.
So my question is how do I query for all events where the date is in the future?
My impression was this might not be possible at the moment.
As far as i Know it is currently Not possible to bring in Custom variables like the current Date to the Query Ui. You would want to create a Meta compare query to compare your ACF field.
Your finished query should look something like this:
$current_date = date('Y-m-d');
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'event_date', // The name of your ACF Field
'value' => $current_date,
'compare' => '>=',
'type' => 'DATE'
)
)
);
So far so good, thanks to the Query Args Filter Cwicly Provides you can still create the query using the UI and only pass the missing parameter with code:
Reference: Filters - Documentation
Add a Code-Block anywhere on your page
in the php part, add the following (replace the query id with your query id)
add_filter( 'cwicly/query/args', function( $query_args, $attributes, $id ) {
// Change the ID in the next line:
if ($id === 'query-c0d6112') {
$query_args['meta_query'][0][0]['value'] = date('Y-m-d'); // Thats the part where the necessary Key gets added
}
return $query_args;
}, 10, 3 );
That snippet is untested, but it should work like this.
Delete what you have placed inside the “Meta Value” Area and leave it blank.
Then, you have to place a “Code” Block anywhere on your page (outside the query element)… there you have to paste the following code:
add_filter( 'cwicly/query/args', function( $query_args, $attributes, $id ) {
// Change the ID in the next line:
if ($id === 'query-c0d6112') {
$query_args['meta_query'][0]['value'] = date('Y-m-d'); // Thats the part where the necessary Key gets added
}
return $query_args;
}, 10, 3 );
You have to replace the query-c0d6112 with the ID of your Query then and everything should work!
Below my query block I placed a code block with the snippet you provided. I changed the ID to query-cfb1ed8:
<?php
add_filter( 'cwicly/query/args', function( $query_args, $attributes, $id ) {
// Change the ID in the next line:
if ($id === 'query-cfb1ed8') {
$query_args['meta_query'][0]['value'] = date('Y-m-d'); // Thats the part where the necessary Key gets added
}
return $query_args;
}, 10, 3 );
?>
This works, but only if I place it in the themes functions.php file. It does nothing when I put it in a code block on the site in question.
Any idea why that is?
But other than that I think the functions.php file works fine too. So thank you very much for your help @Wolfgang !!
Assigning the date to a variable first shouldn’t make any difference.
Normally it should also work with the code block, you can try to debug by using print_r($query_args) inside the code (outside and inside the if clause) and see what’s happening.
But yeah, functions.php is fine too, make sure that you have a child theme installed, cause otherwise it could get overwritten on updates!
I was sure I tried everything with your snippet. I also put it in the functions.php but it did not work. I assume I forgot to check the block ID because as you noted it works also without the variable. I just tried it again and your snippet works . Sorry, my mistake.
Yeah, I thought about the child theme – which I did not install (until know there is no clear way to to this with the Cwicly theme, is there?) – I’ll try to figure out how I can make it work in the page.
As far as I know there is a child theme ready for download, but I could be wrong on that.
You can easily create your own child theme without any coding knowledge, there are dozens of articles out there. I can send you a tutorial later this evening if you don’t find one!
@Wolfgang
I just found out that the code block needs to be somewhere above the query on the page. Then it works. So no need for the functions.php
I usually don’t have anything for the functions.php so I was not worried about the missing child theme so far. The issue is though, that changing the theme to the child theme after a website is already build would remove all the templates and settings. So I will leave it as is for now
Hi @Jonas, for new sites, you can use this child theme - I don’t recommend installing it on an existing site without certain preparations because you may lose some of your customisations: