Skip Navigation

[Resolved] Filtering by date field from CPT (The Events Calendar)

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by peterI 4 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1538363
ingersoll-toolset-ticket.jpg

Hello,

I'm trying to filter events with a start date greater than today. I'm using the field _EventStartDate (I also tried _EventStartDateUTC).

This doesn't work:
Select items with field: _EventStartDate is a UNSIGNED greater than TODAY()

I've tried everything (I think) for the type. I expected that "UNSIGNED" would work - but it returns no results.

Here's the test page: hidden link
It's the block of four at the top. You'll see I have two event that are past, and two in the future. I only want to show the two in the future (formatting to follow, of course).

Also just FYI, I'm using a shortcode to format the _EventStartDateUTC) field in the output, based on this thread:
https://toolset.com/forums/topic/can-i-convert-a-custom-fields-date-format/

So the loop item creating the output is this:

[wpv-post-field name="_EventStartDateUTC" format="j"]<br />
<br />
[change_date date="[wpv-post-field name='_EventStartDateUTC']" format='M']<br />
[change_date date="[wpv-post-field name='_EventStartDateUTC']" format='j']<br />
[wpv-post-link]<br />

Thanks for you help.

- Peter

#1538689

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screenshot 2020-03-04 at 11.35.30.png

The problem is the format the Events Calendar stores its date fields in (screenshot).

Types date fields are stored as UNIX timestamps, and when you use something like TODAY() in a filter that generates a timestamp for the comparison (expecting the comparison to be against a Types date field).

You can create a custom shortcode which will return today's date in the same format as the Events Calendar, and in your Query Filter for the event date specify that the filter value should come from a shortcode attribute.

Then, where you insert the View—using a wpv-view shortcode—add that attribute where the value is set by your custom shortcode.

You'll likely need to register this 3rd party shortcode for use as a shortcode attribute at Toolset > Settings > Front-end Content.

See https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/ for more information about using shortcode attributes to pass values to Views filters.

#1538977

Hello,

While I understand the issue now (and its what I suspected), what I don't know is how to "create a custom shortcode which will return today's date as the Events Calendar."

To be able to pull date parts (month and day), I used this, which I found in Toolset support archives:

// Add Shortcode
function change_date( $atts ) {
   
    // Attributes
    $atts = shortcode_atts(
        array(
            'date' => '',
            'format' => '',
        ),
        $atts
    );
   
    $originalDate = $atts['date'];
    $newDate = date($atts['format'], strtotime($originalDate));
    return $newDate;
   
}
add_shortcode( 'change_date', 'change_date' );

Is there code that will convert today's date to the Events Calendar format (or vice versa) - either that you can help with or point me to a resource?

Thank you.

#1539075

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

This will register a simple shortcode "now" that returns the current date and time in the same format you see used by The Events Calendar:

add_shortcode('now', function () {

    return date( 'Y-m-d H:i:s' );
});
#1539159
shortcode-query.jpg

Thanks for your continued help.

I feel I'm close and hopefully it's just a syntax error. I couldn't find exact syntax for passing shortcode through view shortcode.

This did not work:
[wpv-view name="next-two-events" now="[now]"]

Also, filter looks like attached image.

Result is no filter, with a trailing "] appearing on the page - so the wp-view shortcode obviously isn't correct.

- Peter

#1539217

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Did you remember to register the custom shortcode at Toolset > Settings > Front-end Content?

#1539241

I did not - and that was the final trick in this bag of tricks!

I think it's working for me now. Thanks for your help!

I may put together a tutorial on this. I spent a day looking for ways of getting The Events Calendar in a simple format I wanted, e.g. using premium plugins. I had what I needed all along - I just didn't see it (starting with the _hidden-fields thing).

Now on to formatting it...

#1539245

Actually, one more question: Is there a way to offset the query/filter results by a specified number.

Depending on the formatting challenges (and being pressed for time) I was thinking of duplicating the query but have one view show just the next event and then the second view show the event after that.

#1539335

Please ignore that last request regarding offset. It's right in front of me. I've had three hours of sleep...

#1539337

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.