Skip Navigation

[Resolved] How to use wpv_filter_query

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

Problem:

I have a custom post type "events" on my site.
The events have custom fields for START DATE and END DATE.

On the Archive page I have a working filter that allows users to choose only events between the above dates.

By default it shows ALL events which is unnecessary since some of the events have expired.

i.e. we only want to show events with an END DATE greater than NOW and let visitors filter according to their preference.

Solution:

Since the problem URL is post type archive page, the query is defined by wordpress, so you will need to use WordPress built-in action hook pre_get_posts to change the query, for example:

https://toolset.com/forums/topic/how-to-use-wpv_filter_query/#post-1206145

Relevant Documentation:

https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 10 replies, has 2 voices.

Last updated by PaulS4783 5 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1205338

I have a custom post type "events" on my site.
The events have custom fields for START DATE and END DATE.
On the Archive page I have a working filter that allows users to choose only events between the above dates.
By default it shows ALL events which is unnecessary since some of the events have expired.
i.e. we only want to show events with an END DATE greater than NOW and let visitors filter according to their preference.

See ticket:
https://toolset.com/forums/topic/how-to-sort-and-limit-custom-post-type-archive-listing/

I understand that we should be able to do this with the wpv_filter_query in functions.php
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

I can only get this far with the code.

Can you advise what the "//do something here" portion should be?

//Return events which have not expired i.e. with END dates greater than NOW:
add_filter( 'wpv_filter_query', 'hide_expired_events', 99, 3 );
function hide_expired_events( $query_args ) {
    
    //do something here
    
}
#1205374

Hello,

Yes, you are right, you can put your custom codes in the "//do something here" portion, for example:

Add below codes into your theme file "functions.php"

add_filter( 'wpv_filter_query', 'hide_expired_events', 99, 3 );
function hide_expired_events( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 123 && !isset($_GET['wpv_filter_submit']) ) { // if it is specific view and by default
        $query_args['meta_query'][] = array(
				'key'     => 'wpcf-end-date', 
				'value'   => current_time('timestamp'),
				'compare' => '>=',
		);
    }
    return $query_args;
}

Please replace 123 with your view's ID

More help:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Custom Field Parameters
https://codex.wordpress.org/Function_Reference/current_time
Returns the blog's current local time in the specified format

#1205405

Unfortunately didnt work.
Can I grant you FTP and site access to take a look?

#1205419

Yes, please provide the credentials in below private message box

#1206026

Thanks for the details, since the problem URL is post type archive page, the query is defined by wordpress, so you will need to use WordPress built-in action hook pre_get_posts to change the query, I have have edited your theme file functions.php. change the codes to:

//Return events which have not expired i.e. with END dates greater than NOW:
add_filter( 'pre_get_posts', 'hide_expired_events' );
function hide_expired_events( $query ) {
    if ( is_post_type_archive('event') && $query->is_main_query() && !isset($_GET['wpv_filter_submit']) ) { // if it is event archive page and by default
		$meta_query = $query->get('meta_query');
        $meta_query[] = array(
                'key'     => 'wpcf-end-date', 
                'value'   => current_time('timestamp'),
                'compare' => '>=',
        );
		$query->set('meta_query', $meta_query);
		return;
    }
}

Please test again, check if it is fixed.

More help:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

#1206131

It works fine on the first visit.
i.e. by default it only shows events which have not expired

BUT when a visitor uses any of the search filter parameters it also returns expired events.
e.g. a search for event type "Exhibition" returns an event that has already ended.

Ideally, I would like to NEVER show expired events on this page regardless of the query parameters.
Is that possible?

#1206133

You can modify this line from:

if ( is_post_type_archive('event') && $query->is_main_query() && !isset($_GET['wpv_filter_submit']) ) { // if it is event archive page and by default

To:

if ( is_post_type_archive('event') && $query->is_main_query() ) { // if it is event archive page

And test again.

#1206138

Nailed it!

Thank you. You can close the ticket.

#1206141

Hmmm. ... spoke a little too soon?

It works fine in:
hidden link

BUT

hidden link

throws fatal errors. 🙁

#1206145

Thanks for the feedback, I have modified the codes to below:

//Return events which have not expired i.e. with END dates greater than NOW:
add_filter( 'pre_get_posts', 'hide_expired_events' );
function hide_expired_events( $query ) {
    if ( is_post_type_archive('event') && $query->is_main_query() ) { // if it is event archive page
		$meta_query = $query->get('meta_query');
		if(!is_array($meta_query)){ // by default
			$meta_query = array();
		}
        $meta_query[] = array(
                'key'     => 'wpcf-end-date', 
                'value'   => current_time('timestamp'),
                'compare' => '>=',
        );
		$query->set('meta_query', $meta_query);
		return;
    }
}

Please test again, check if it is fixed. thanks

#1206173

You got it.
All working perfectly now.

Thank you so much.