Skip Navigation

[Resolved] Custom search for Upcoming Events & Previous Events Using Dropdown

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

Problem: I would like to include a custom search filter that allows users to filter by past events or future events. This is the only filter I would like to include, and I want the results to update with AJAX.

Solution:
1. Add a custom select field to your View's Filter Controls editor. If this section is not visible, you must enable it in the Screen Options tab at the top of the admin screen. Insert the following code:

<select name="custom-end-filter" id="custom-end-filter" class="js-wpv-filter-trigger form-control">
  <option value="">All</option>
  <option value="1">Upcoming</option>
  <option value="2">Past</option>
</select>

2. Add the following custom code to functions.php:

add_filter( 'wpv_filter_query', 'special_end_filter', 99, 3 );
function special_end_filter( $query_args, $view_settings, $view_id ) {
  $the_end = isset($_GET['custom-end-filter']) ? $_GET['custom-end-filter'] : '';
  if( $view_id == 12345 && $the_end ) {
    $compare = $the_end == '1' ? '>' : '<';
    $args = array(
      'relation' => 'AND',
      array(
        'key' => 'wpcf-the-end',
        'value' => strtotime('23:59:59'),
        'compare' => $compare,
        'type' => 'numeric'
      )
    );
    // add these arguments to your meta query
    $query_args['meta_query'] = isset($query_args['meta_query']) ? $query_args['meta_query'] : [];
    $query_args['meta_query'][] = $args;
  }
  return $query_args;
}

Replace 12345 with your View's numeric ID, and replace 'wpcf-the-end' with the slug of your end date field. Use the 'wpcf-' prefix here.

3. Add a standard filter to this View's Filter Controls and hide it with CSS. Example Category filter:

<div class="form-group" style="display:none;">
    <label>[wpml-string context="wpv-views"]Categories[/wpml-string]</label>
    [wpv-control-post-taxonomy taxonomy="category" type="select" url_param="wpv-category"]
</div>

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

This support ticket is created 7 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by marcB-6 7 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#569606

I am having view for all events in events page..now I would like to add filter using dropdown.
Dropdown would have 2 values
1)Upcoming events : The upcoming events overview lists all published events that have an enddate ({enddate}
23:59) in the future.
2)Previous Events : The preview events overview lists all published ICISA events that have an enddate ({enddate}
23:59) in the past.

Based on selection from dropdown I will see results for upcoming event and previous event.

How can I achieve above goal using toolset view filters?

Thanks

#569643

You need to follow these steps:

1. Add a custom select field to your View's Filter Controls editor. If this section is not visible, you must enable it in the Screen Options tab at the top of the admin screen. Insert the following code:

<select name="custom-end-filter" id="custom-end-filter" class="js-wpv-filter-trigger form-control">
  <option value="">All</option>
  <option value="1">Upcoming</option>
  <option value="2">Past</option>
</select>
[/wpv-filter-controls]
[wpv-filter-end]

2. Add the following custom code to functions.php:

add_filter( 'wpv_filter_query', 'special_end_filter', 99, 3 );
function special_end_filter( $query_args, $view_settings, $view_id ) {
  $the_end = isset($_GET['custom-end-filter']) ? $_GET['custom-end-filter'] : '';
  if( $view_id == 12345 && $the_end ) {
    $compare = $the_end == '1' ? '>' : '<';
    $args = array(
      'relation' => 'AND',
      array(
        'key' => 'wpcf-the-end',
        'value' => strtotime('23:59:59'),
        'compare' => $compare,
        'type' => 'numeric'
      )
    );
    // add these arguments to your meta query
    $query_args['meta_query'] = isset($query_args['meta_query']) ? $query_args['meta_query'] : [];
    $query_args['meta_query'][] = $args;
  }
  return $query_args;
}

Replace 12345 with your View's numeric ID, and replace 'wpcf-the-end' with the slug of your end date field. Use the 'wpcf-' prefix here.

#569644

If this is the only filter in your filter controls area and you intend to use AJAX, there's one additional step. Let me know if that's the case.

#569872

I have put code in my functions.php and view..I want to use AJAX
Please check this url : edited
Please make this as private message as it is having site url..

Thanks

#570066

Okay I've edited your post to remove the URL since I don't have the ability to make a public post private. Next, you need to include an additional filter in the Filter Controls area. Without a standard filter, the AJAX update code will not be triggered. So my recommendation is to add a Category filter (or some other taxonomy related to this post type) and hide it with CSS. Here's an example (note the display:none style):

<div class="form-group" style="display:none;">
	<label>[wpml-string context="wpv-views"]Categories[/wpml-string]</label>
	[wpv-control-post-taxonomy taxonomy="category" type="select" url_param="wpv-category"]
</div>

Once you add this code, you should begin seeing AJAX updates whenever you change the other filter. Let me know if this does not resolve the issue for you.

#570196

Its working ...Thanks a lot Christian Cox!!

#571310

Hi, With reference to above functionality, I would like to add 2 more things..

Upcoming events

1) The events are chronically arranged by startdate with first upcoming events as first event.

Previous events

1)The events are chronically arranged by enddate with most recent enddate as first event.

What needs to be added to the filter to achieve above 2 goals?

Thanks,