Skip Navigation

[Resolved] query filter: future dates only

This support ticket is created 5 years, 10 months 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 8 replies, has 2 voices.

Last updated by Joe H. 5 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#1202100
Screen Shot 2019-02-18 at 5.56.01 PM.png
Screen Shot 2019-02-18 at 5.09.04 PM.png
Screen Shot 2019-02-18 at 5.08.03 PM.png

I am trying to:
use Query Filter in my Events CPT view to only show events with end dates greater than or equal to today
(this CPT is not a toolset CPT but is being created by a 3rd party plugin, I've attached a screen cap of its settings)

Link to a page where the issue can be seen:
hidden link

I expected to see:
My 1 test event with a future date. (all other events are past)

Instead, I got:
I am not seeing any events with my current query settings. All events are being filtered out.

I've tried:
- tried solutions offered in similar support threads
- tried experimenting with different query filter settings

I have also included a screencap from a theme that natively displays future events using this same plugin in case this is helpful. I am essentially trying to recreate this same loop of future events on a different theme using the same plugin + Toolset Views.

#1202421

Nigel
Supporter

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

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

Hi Joe

Types date fields are stored as UNIX timestamps, so when you create a Query Filter using TODAY(), that also uses a timestamp so that you are comparing like-with-like.

From your screenshots it looks like your 3rd party post type and custom fields is storing dates as a date, so the comparison would fail.

In which case, you cannot set up this View without using custom code for the date filter.

You have a very good example in your screenshots of what the meta_query should look like when working with this post type and custom date field, and you need to reproduce that for your View.

You will need the wpv_filter_query hook, and you will need to update $view_arguments with the meta_query that you can see is used by the theme.

See https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query for details of how to use the required hook.

#1202787

Nigel, thank you for your reply. I am a novice with PHP but I am attempting your suggestion. I am not understanding what you mean by "you will need to update $view_arguments with the meta_query that...is used by the theme". Can you please elaborate on this?

Here is my function so far, though I am not sure if this is correct or even close:

// Query events that have end date today or future
function boc_events_upcoming_loop( $args ) {

// Query arguments
$args = array(
'post_type' => 'ctc_event',
'meta_query' => array(
array( // only get upcoming events (ending today or in future)
'key' => '_ctc_event_end_date', // the latest date that the event goes to (could be same as start date)
'value' => date_i18n( 'Y-m-d' ), // today's date, localized
'compare' => '>=', // all events with start OR end date today or later
'type' => 'DATE'
),
),
'meta_key' => '_ctc_event_start_date_start_time', // want earliest start date/time first
'meta_type' => 'DATETIME', // 0000-00-00 00:00:00
'orderby' => 'meta_value',
'order' => 'ASC' // sort from soonest to latest
);
// Return modified query
return new WP_Query( $args );
}
add_filter( 'wpv-filter_query', 'boc_events_upcoming_loop');

********************
And, if this could be modified to work and then added to my child theme functions.php, I am not sure how to modify my View in TS so that this filter gets applied.

#1203330

Nigel
Supporter

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

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

Hi Joe

Sorry, the forum is very busy at the moment and I just haven't had time to reply to this.

I will help you set this filter up as required, just not right now.

I'll get back to you as soon as I can.

#1203481

Ok, thank. I look forward to your next response.

#1203868

Nigel
Supporter

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

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

Hi Joe

1. Delete the Query Filter you added to your View settings, we'll add one programmatically
2. Add the following code to your site. (No need to edit your theme files, you can add code snippets at Toolset > Settings > Custom Code).

function tssupp_filter_query( $view_args, $view_settings, $view_id )  {
    
  if ( in_array( $view_id, array( 99 ) ) ) { // Edit the View ID
    
  	$view_args['meta_query'] = array(
		array( // only get upcoming events (ending today or in future)
			'key' => '_ctc_event_end_date', // the latest date that the event goes to (could be same as start date)
			'value' => date_i18n( 'Y-m-d' ), // today's date, localized
			'compare' => '>=', // all events with start OR end date today or later
			'type' => 'DATE'
		),
	);
  }
  
  return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_query', 101, 3 );

You need to edit the ID of the View this relates to.

We are not creating a WP_Query here, we are modifying the arguments that Views will use to create a WP_Query.

I haven't included the ordering options in that custom code, because I think it should work with your View settings, it shouldn't be necessary to override them.

Can you check that to see if it works?

#1204107

Wow I am sure I never would have been able to create that code myself (and didn't know I could add code to TS this way!). Thank you. I followed all your steps carefully and it does work as long as I select 'run always' instead of 'on demand'.

I do get a warning in the snippet about the security check being missing even though it is not missing, but I am not super concerned about that. But did want to mention.

<?php
function tssupp_filter_query( $view_args, $view_settings, $view_id ) {

if ( in_array( $view_id, array( 15858 ) ) ) { // Edit the View ID

$view_args['meta_query'] = array(
array( // only get upcoming events (ending today or in future)
'key' => '_ctc_event_end_date', // the latest date that the event goes to (could be same as start date)
'value' => date_i18n( 'Y-m-d' ), // today's date, localized
'compare' => '>=', // all events with start OR end date today or later
'type' => 'DATE'
),
);
}

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

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

Thank you very much. I need to keep learning PHP!

#1204391

Nigel
Supporter

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

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

Hi Joe

Yes, this script needs to run all the time, not just once.

Also, I think the warning about the security script is because that line with toolset_snippet_security_check needs to be at the top, you have it at the bottom.

#1204729

Perfect, thanks Nigel!