Query: only show events with Date in the Future

Hi,

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.

1 Like

@Jonas,

Perhaps you can get some inspiration from this thread:

Hey there @Jonas ,

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

  1. Add a Code-Block anywhere on your page
  2. 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.

Let me know if you need any further help! :wink:

Cheers
Wolfgang

1 Like

Hi @Wolfgang,

thank you very much for your detailed response!!
Unfortunately I don’t really know anything about php so please excuse my probably dumb questions.

I created this Meta Query:

In the code it looks like this:

$args = array (
  'post_type' => 
  array (
    0 => 'messe',
  ),
  'ignore_sticky_posts' => true,
  'meta_query' => 
  array (
    0 => 
    array (
      'key' => 'messe_bis',
      'value' => '$current_date',
      'compare' => '>=',
      'type' => 'DATE',
      'title' => 'datum',
    ),
  ),
  'orderby' => 'meta_value',
  'meta_key' => 'messe_von',
  'order' => 'ASC',
);
// The Query
$the_query = new WP_Query( $args );

So the $current_date is placed as a string (I guess that’s what it is) rather than a variable, don’t know, how to change that.

Also, how do I get this line:

$current_date = date('Y-m-d'); 

Hey @jonas,

You almost got it.

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!

Cheers

2 Likes

Hi @Wolfgang,

thanks again for your effort! I really appreciate it!

Unfortunately something is not right though.

My Query configuration now looks like this:


Here is the code generated of the query:

<?php 
// The Arguments
$args = array (
  'post_type' => 
  array (
    0 => 'messe',
  ),
  'ignore_sticky_posts' => true,
  'meta_query' => 
  array (
    0 => 
    array (
      'key' => 'messe_bis',
      'compare' => '>=',
      'type' => 'DATE',
      'title' => 'datum',
      'value' => 
      array (
      ),
    ),
  ),
  'orderby' => 'meta_value',
  'meta_key' => 'messe_von',
  'order' => 'ASC',
);
// The Query
$the_query = new WP_Query( $args );

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 );
?>

Do you have any further idea what I could try?

I asked Chat GPT for help and it gave me this code:

add_filter( 'cwicly/query/args', function( $query_args, $attributes, $id ) {
    if ($id === 'query-cfb1ed8') {
        $current_date = date( 'Y-m-d' );

        $query_args['meta_query'][0]['value'] = $current_date;
    }

    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 !!

Hey @Jonas,

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! :wink:

Hi @Wolfgang,

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 :champagne: :slight_smile:. 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?) :expressionless: – I’ll try to figure out how I can make it work in the page.

Thanks again for your great help! :slight_smile:

@Jonas Hmm yeah that’s really a bit weird :wink:

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! :wink:

@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 :slight_smile:

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 :slight_smile:

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: