Skip Navigation

[Resolved] Checkbox filter listing event start date (Years only)

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Minesh 1 month, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2771180
news-post-date-filter.jpg
news-years-checkboxes.jpg
event-desired-result.jpg

Hi,

We are using the Event Calendar pro plugin to create events and using the Toolset View legacy version to create a view with filters for events.

We want to create a checkbox filter that lists all the upcoming event start dates ('Years' only).

I have setup a couple of test events:

- Event 1 (start date: Oct 31, 2024)
- Event 2 (start date: Nov 1, 2024)
- Event 3 (start date: Jan 04, 2025)
- Event 4 (start date: Jan 25, 2025)

So the checkbox filter will have these 2 years (screenshot event-desired-result.jpg):
- 2024
- 2025

For our News page, we also have a checkbox filter that lists all the post published date (years only) by using the 'Query Filter' , Post date filter (screenshot news-years-checkboxes.jpg + news-post-date-filter.jpg)

In the 'search and pagination' section, I just added this and the filter works:
[wpv-control url_param="wpv-year" type="checkboxes" output="legacy" values="2024,2023,2022,2021,2020,2019,2018"]

For my events page, is there a way (similar to news) to display the event start years without having to create a custom taxonomy for years?

I assume it's using ' _EventStartDate', but not sure how to set that up.

Thanks!

#2773912

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The Toolset's date filters are designed to work with the values in UNIX timestamp format. That is the same format in which Toolset's own date-type field stores the values. So, for instance if you have date custom field created using Toolset the value of this date custom field will be stored as Unix Timestamp in the database table.

The Events Calendar plugin on the other hand stores the event date values as date string i.e. human-readable formats.

There are couple of ways we can do this: You can create a custom field that will hold the year value and when you save/update post you should try to extract the year value from the "_EventStartDate" field and save it to this custom field and then add this custom field as search filter to your view.

If you want to make this work using the way you shown:

In the 'search and pagination' section, I just added this and the filter works:
[wpv-control url_param="wpv-year" type="checkboxes" output="legacy" values="2024,2023,2022,2021,2020,2019,2018"]

Then I need admin access details and problem URL where you added the view and I will try to help you to make it working.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2776269

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've added the following custom search filter to your view's "Search and Pagination" section:
=> hidden link

<div class="form-group">
	<label for="wpv-_EventStartDate">[wpml-string context="wpv-views"]Event Year[/wpml-string]</label>
	[wpv-control-postmeta display_values="2025,2024,2023,2022,2021,2020,2019,2018" values="2025,2024,2023,2022,2021,2020,2019,2018" field="_EventStartDate" type="checkboxes" source="custom" url_param="wpv-_EventStartDate"]
</div>

And to filter the events by year I've added the following hook code to "Custom Code" section offered by Toolset:
=> hidden link

add_filter('wpv_filter_query', 'func_filter_tribe_events_by_year', 99, 3);  
function func_filter_tribe_events_by_year($query_args, $view_settings, $view_id) {
  
  global $WP_Views;
  global $wpdb;
       
          
if($view_id == 2692)  {
  
 
   if(defined( 'DOING_AJAX') and DOING_AJAX ) {
  
     	$query_args['meta_query'] = array();
        foreach($_POST['search']['dps_general'] as $k=>$v):
            if($v['name']=='wpv-_EventStartDate[]'){
              
              if($k>=1){
                 $meta_qry_str .= " OR ";
              }
   	$meta_qry_str .= "(".$wpdb->prefix."postmeta.meta_key = '_EventStartDate' 
    				AND YEAR(".$wpdb->prefix."postmeta.meta_value) = ".$v['value'].")";
                
             
                }
        endforeach;
     
      $sql = "SELECT post_id FROM ".$wpdb->prefix."postmeta,".$wpdb->prefix."posts  
                WHERE (".$wpdb->prefix."postmeta.post_id = ".$wpdb->prefix."posts.ID) 
                AND ".$meta_qry_str."
                AND ".$wpdb->prefix."posts.post_type = 'tribe_events'
                AND ((".$wpdb->prefix."posts.post_status = 'publish' 
                OR ".$wpdb->prefix."posts.post_status = 'private'))";
        
     $res = $wpdb->get_col($sql);
  
        if(!empty($res)){
            $query_args['post__in'] = $res;
        }else if($meta_qry_str!='' and empty($res)){
           $query_args['post__in'] = 0;
        }
   
       
   } 
  
   }
return $query_args;
}

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

Can you please confirm it works as expected now.

#2776847

Thank you so much Minesh! Your code works!!