Skip Navigation

[Resolved] Filter view between post dates compared with shortcode attributes

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

Problem:

I have an odd thing happening with one of my views that should get posts that are published between two publish dates. The dates are set by a shortcode attribute and a custom date field set by the current page (post). It works fine for any set of dates EXCEPT if the date range crosses over from one month to the next month.

Solution:

It is a limitation of WP_Query, when the report-start "day" value is larger than the report-end "day" value, it will conduct the problem you mentioned above.

In your case, I suggest you setup the View filter with wpv_filter_query filter hook, for example:

https://toolset.com/forums/topic/filter-view-between-post-dates-compared-with-shortcode-attributes/#post-2108303

Relevant Documentation:

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

https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

This support ticket is created 2 years, 9 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by Rita 2 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#2108213
Screenshot 2021-07-07 at 08.29.12.png

Hi

I have an odd thing happening with one of my views that should get posts that are published between two publish dates. The dates are set by a shortcode attribute and a custom date field set by the current page (post). It works fine for any set of dates EXCEPT if the date range crosses over from one month to the next month.

For example:

25 June 2021 to 30 June 2021 - Yes. Works fine...
01 July 2021 to 05 July 2021 - Yes. Works fine...

but...
25 June 2021 to 05 July 2021 - No posts found...
25 June 2021 to 30 July 2021 - No posts found...

no end dates in July work...
25 June 2021 to 31 July 2021 - Yes. Works fine...

Here is the setup:

1. When a user creates a 'REPORT' (toolset post type) post they must set a custom field 'start-date' and 'end-date' using the date-picker in a cred form.

2. Then on the 'REPORT' post is a view of 'ORDERS' posts (woo post type) filtered by 'published date Year', 'published date Month' and 'published date Day' using the 'between' comparison set by the shortcode attributes for the report's 'start-date' and 'end-date'.

2a. The view is set to 'ORDER' posts by date (ascending), has a filter to "Select posts with status of publish or wc-completed or wc-refunded." and has a filter to only show posts between two published dates. Attached is a screenshot of the views date settings.

2b. Here is the view shortcode on the 'REPORT' content template:
[wpv-view name="report-list-order" yearstart="[types field='report-start' style='text' format='Y'][/types]" monthstart="[types field='report-start' style='text' format='n'][/types]" daystart="[types field='report-start' style='text' format='j'][/types]" yearend="[types field='report-end' style='text' format='Y'][/types]" monthend="[types field='report-end' style='text' format='n'][/types]" dayend="[types field='report-end' style='text' format='j'][/types]"]

Do you think there is a bug? Or something wrong with my setup? I always presume there is something wrong with my setup...

Hoping you can help!

Rita

#2108303

Hello,

It is a limitation of WP_Query, when the report-start "day" value is larger than the report-end "day" value, it will conduct the problem you mentioned above.

In your case, I suggest you setup the View filter with wpv_filter_query filter hook, for example, add below codes into your theme file "functions.php":

add_filter('wpv_filter_query', function($query_args, $view_settings, $view_id){
	if(in_array($view_id, array(123))){
		$post_id = get_the_ID();
		$report_start = types_render_field("report-start", array("format" => "Y-m-d"));
		$report_end = types_render_field("report-end", array("format" => "Y-m-d"));
		$query_args['date_query'] = array(
			'after' => $report_start,
			'before' => $report_end,
		);
	}
	return $query_args;
}, 999, 3);

Please replace 123 with your view's ID, and test again

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

#2109135

Thank you Luo! That resolves the issue perfectly.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.