Skip Navigation

[Resolved] Views filter date by a specific month

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

Problem:
How to filter view result by a specific month of custom date field

Solution:
Well - you should use the view's shortcode attribute to filter view result by specific month with some adjustment in view's query using view's filter wpv_filter_query.

For exmaple:

[wpv-view name="view-filter-by-month" month="1"] // if January

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/views-filter-date-by-a-specific-month/#post-953546

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
=> https://toolset.com/documentation/user-guides/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes

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

Last updated by kaneB 6 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#947756

I have CPT with a date field. How do I create a view that filters only the post with a specific month, say July. The year and day is not relevant. I have looked at https://toolset.com/documentation/user-guides/date-filters/ but couldn't figure it out.

Thank you.

#948235

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - when you say you want to filter view with specific month, do you need a dropdown select filter that lists months as options or you just want to have static July month filter and display posts belongs to July month?

#949180

I would need guidance for both - filter by dropdown selection (month only) and more importantly for my current purpose, a static view to display a particular month's post.

#950002

Hello,

Minesh isn't available, I will take care this thread.

For your question:
I have CPT with a date field. How do I create a view that filters only the post with a specific month, say July. The year and day is not relevant

Yes, you are right, there isn't such a feature within Views date field filter, see the document you mentioned above:
https://toolset.com/documentation/user-guides/date-filters/

No option to filter the posts by month(July)

If you agree, we can take it as a feature request, our developers will evaluate it.

But it is possible with Views filter on publish date, see our document:
https://toolset.com/documentation/user-guides/filtering-views-query-by-date/
Month values go from 1 to 12

And there isn't filter by dropdown selection in both custom date field and publish date.

#952357

Is there a way to do a quick shortcode to return the month of the custom date field? The perhaps I could use the shortcode attribute in the views filter.

#953546

Minesh
Supporter

Languages: English (English )

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

Luo is on vacation and I'm back to handle this ticket.

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', 'remove_past_events', 10, 3);  
function remove_past_events($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

#954489

Just wondering if this code will work if my date field is a repeatable field?

The code is not working as it should be now.

#954496

Minesh
Supporter

Languages: English (English )

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

NO - that is a different story, you didn't mentioned that the date field is repeating field.

#956606

I tested further and noted the code is working when I insert the shortcode onto a content (page or post), but not when it's in another view.

I will raise a new ticket to address the filtering with repeated field. Thanks.