Skip Navigation

[Resolved] the events calendar recurrring events view

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to add a filter to show only future events from the Events Calendar plugin in a View?

Solution:
The start date is stored as in the field "_EventStartDateUTC" but it is not stored in the timestamp format used by Types date fields, and to filter by this field you will need to use the wpv_filter_query hook.

Here is an example of such code (the View ID would need editing):

/**
 * Filter event query to show future events
 */
function tssupp_filter_event_query($view_args, $view_settings, $view_id) {
 
  if ( in_array( $view_id, array( 123 ) ) ) { // Edit View ID(s)
 
    if (!isset($view_args['meta_query'])) {
 
      $view_args['meta_query'] = array();
    }
 
    $meta_query = array(
 
      'key' => '_EventStartDateUTC',
      'value' => date("Y-m-d H:i:s"),
      'type' => 'DATETIME',
      'compare' => '>',
    );
 
    $view_args['meta_query'][] = $meta_query;
 
  }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_event_query', 101, 3);

The thread also includes an example of code that would be used to filter between a range of dates.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 6 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.

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+00:00)

This topic contains 11 replies, has 2 voices.

Last updated by Ido Angel 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1153344

hey guys,
hope this one isn't out of your scope.

i'm using "the events calendar" plugin an i have a view showing events.
i managed to get to show the event start date using the custom field "_EventStartDateUTC" - but - i can't filter with this field.
the reasons i want to filter are:
1. i want to only show future events
2. when i create a recurring event, the view treats each instances as a standalone event, then i can get about 200 events in the view. the events calendar have an option to present only the upcoming event in a series, but this option doesn't work on the view, which keeps showing me all instances.

is there any way around this?

the only solution i could think is to create a new types custom field of "start-date" and have this somehow copied from the "_EventStartDateUTC" field when a new event is created. then, these fields will be the same, but the one i created with types will be filterable.

unless you have another idea.

thanks!

ido

#1153631

Nigel
Supporter

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

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

Why can't you filter by _EventStartDateUTC?

Because you can't find it in the list of fields to filter, or some other reason?

Custom fields that start with an underscore are hidden.

You can go to Toolset > Settings > Front-end Content and in the first section choose hidden fields that you want to be able to work with.

#1153635

hey nigel,
thx 🙂

the field is already visible and i'm using it in the view to display the date. but whenever i try to filter with it - i can 0 results.
i tried filtering it as date, datetime, number (which is the option to be used according to your documentation) - but nothing.

ido

#1153709

Nigel
Supporter

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

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

Screenshot 2018-11-27 at 08.55.31.png

I looked in wp_postmeta and see that the dates are not stored as timestamps (which is how Types date fields are stored, which is why the comparison should be number), but what looks like datetime (see screenshot).

Remove the Query Filter you added in the Views GUI and add one programmatically using the wpv_filter_query hook like so:

/**
 * Filter event query to show future events
 */
function tssupp_filter_event_query($view_args, $view_settings, $view_id) {

  if ( in_array( $view_id, array( 123 ) ) ) { // Edit View ID(s)

    if (!isset($view_args['meta_query'])) {

      $view_args['meta_query'] = array();
    }

    $meta_query = array(

      'key' => '_EventStartDateUTC',
      'value' => date("Y-m-d H:i:s"),
      'type' => 'DATETIME',
      'compare' => '>',
    );

    $view_args['meta_query'][] = $meta_query;

  }
	return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_event_query', 101, 3);

You'll need to edit the ID(s) of the View(s) you want this to operate on.

#1153728

thanks for all the effort, dear nigel!

unfortunately, it still doesn't work.

1) it doesn't filter the events to be larger than now
2) since the view shows both events and articles, in "articles" i get no results now

question for later: how would i go about using this code to filter events which are larger than today but smaller than 30 days ahead?

thanks!

#1153825

Nigel
Supporter

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

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

Screenshot 2018-11-27 at 12.17.16.png

I'm surprised it doesn't work, it works correctly on my test site, so I'm not sure what to suggest aside from make sure you edited the ID of the View, and if you are still having problems, go to Toolset > Settings > Front-end content and turn on the debug mode for Views. Then reload the page with the View and in the pop-up check the wpv_query_filter arguments have been applied, as shown in the screenshot.

In that case I'm applying some modified code where I updated the code for a date range.

That code would look like this:

/**
 * Filter event query to show future events
 */
function tssupp_filter_event_query($view_args, $view_settings, $view_id) {

	if (in_array($view_id, array(189))) {

		if (!isset($view_args['meta_query'])) {

			$view_args['meta_query'] = array();
		}

		$meta_query = array(
			'relation' => 'AND',
			array(
				'key' => '_EventStartDateUTC',
				'value' => date("Y-m-d H:i:s"),
				'type' => 'DATETIME',
				'compare' => '>',
			),
			array(
				'key' => '_EventStartDateUTC',
				'value' => date("Y-m-d H:i:s", time() + 30 * 24 * 60 * 60),
				'type' => 'DATETIME',
				'compare' => '<',
			),
		);

		$view_args['meta_query'][] = $meta_query;

	}
	return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_event_query', 101, 3);

If your View is showing two post types and one of the post types doesn't have the _EventStartDateUTC custom field then those posts will necessarily be excluded from the results.

#1153879

dear nigel - thanks - the new code works.
unfortunately, because i really have two post types there and one doesn't have the _EventStartDateUTC custom field - i get 0 results on that type.
unless there's a way to limit your code to a certain post type (or better - a certain category within this post type) - then i'll have to let this go and just live with what i have.
thanks for all the effort - you're the best!
ido

#1153884

Nigel
Supporter

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

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

Logically, what you want is not possible within a single query.

You are saying "show me events and articles with a future start date" but your articles have no start date so will always fail that test.

There isn't a way in a single query to say "show me articles and also show me events with a future start date", you would need two separate queries in that case, one for articles and one for events.

#1153904

ok nigel, thx. i appreciate everything.

#1153934

i'm back! ok, so i made a search only for events. how do i control the time section? meaning - from now until X time ahead?
thanks!

#1153950

Nigel
Supporter

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

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

The second query has the value based upon time() + 30 * 24 * 60 * 60, which is the current time plus 30 days (in seconds).

#1153951

My issue is resolved now. Thank you!