Skip Navigation

[Resolved] SELECTING A LIST OF EVENTS WITH DATE = THIS YEAR

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

Problem:
SELECTING A LIST OF EVENTS WITH DATE = THIS YEAR

Solution:
As custom date field value stored as UNIX timestamp, you need to use view's filter wpv_filter_query to filter date by month and year.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/selecting-a-list-of-events-with-date-this-year/#post-1112870

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

This support ticket is created 6 years, 3 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
- 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 6 replies, has 2 voices.

Last updated by Silvia 6 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1111584
filterbydate.png

Hello, I am building an event management website for my company that assigns different events (and tasks in order to plan those events ) to different workmates, based on their department. Every event has a custom field Event Date which is a Types Date field. I would like to create a view that filters events by YEAR based on the event date, tho show events occurring, say, in 2018. Since I would like it to be a dynamic view, I didin't use "constant" and instead I tried to use the "date equals UNSIGNED This_Year as indicated in https://toolset.com/documentation/user-guides/date-filters/ but it's not working. What am I doing wrong? Since I need to create a few more views filtering by date parts, I guess once I have seen how to do this kind of comparison with year, then it will be the same for every element of the date.

#1112062

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - I would like to know from which year to which year you want to filter your results?

Do you want a filter OR you want multiple views where you are passing a year to filter by?

#1112118

Hello and thanks for your reply. I will try to clarify my problem for you a little better.
Because every user has different tasks assigned to them based on their role in the Company in order to plan an event, I have a "dashboard" page in the website, whose content is based on the user logged in at the moment. I want to be able to use Views to display different events based on certain conditions. Example: We're in September and it's the year 2018; the user that visit the dashboard today should be able to see a view with all the events occurring this month, and a view with all the events planned for 2018 or maybe a view with all the events that took place in August.
I would like to know if it's possible to achieve this filtering Events dynamically by a certain year or month based on the "Event Date" custom field I created. The field is of "DATE" type. How can I tell Views "filter all the events whose month in the Event Date corresponds to the current month/year/day"? I thought I could make the comparison using "EVENT DATE IS AN UNSIGNED ELEMENT EQUAL TO THIS_YEAR()/THIS_MONTH()/NEXT_YEAR() etc..." but it's not working. Is there a way to achieve this via Views filters?

#1112870

Minesh
Supporter

Languages: English (English )

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

Well - to filter a view using month, for example:

[wpv-view name="view-filter-by-month" month="1"]   // for january
 
[wpv-view name="view-filter-by-month" month="3"]   // for march

As custom date field value stored as UNIX timestamp, you need to use view's filter wpv_filter_query .

For example - please add following code to your current theme's functions.php file:

add_filter('wpv_filter_query', 'func_filter_by_month', 10, 3);  
function func_filter_by_month($query_args, $view_settings, $view_id) {
 
  global $WP_Views;
  global $wpdb;
      
         
if($view_id == 9999)  {
     $passed_month =  $WP_Views->view_shortcode_attributes[0]['month'];   // getting shortcode attribute value
 
      $sql = "SELECT post_id FROM ".$wpdb->prefix."postmeta,".$wpdb->prefix."posts  
                WHERE (".$wpdb->prefix."postmeta.post_id = ".$wpdb->prefix."posts.ID)
                AND ".$wpdb->prefix."postmeta.meta_key = 'wpcf-REPLACE-DATE-FIELD-SLUG'
                AND month(FROM_UNIXTIME(".$wpdb->prefix."postmeta.meta_value)) = ".$passed_month."
                AND ".$wpdb->prefix."posts.post_type = 'student'
                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;
             
        }
   }
return $query_args;
}

Where:
- Replace 9999 with your original view ID,
- REPLACE-DATE-FIELD-SLUG with your original custom date field slug
- Replace student with your post type slug

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#1114107

Can you be a little more specific on how to go from creating a view to incorporating the code you provided? And in your example it looks like I know the month I wanna query, but what if I want that month to be dynamically set based on the current month?

#1114206

Minesh
Supporter

Languages: English (English )

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

Can you be a little more specific on how to go from creating a view to incorporating the code you provided?
==> Well - the code I shared, you need to add it to your current theme's functions.php file. Do not forget the replace original view ID and other stuff.
==> Setup your view and build your view's loop output and save your view.

And in your example it looks like I know the month I wanna query, but what if I want that month to be dynamically set based on the current month?
=> If you want to use dynamically - the current month. You can use date function.

$current_month  =  date('n');

And replace $passsed_month with $current_month code.

#1114226

When I asked you to be more specific I meant it wasn't clear from the code you provided how to use the shortcode. You didn't specify that "view-filter-by-month" was an example name and had to be replaced with my actual view name. Anyway I figured it out myself, and it's working. I will try to further customize those functions in order to query the database with current month and year. My issue is resolved now. Thank you!