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.
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?
[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
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?
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.
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!