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
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
Thank you Luo! That resolves the issue perfectly.