Problem: I have a View of posts that include a custom date field. I would like to sort and paginate the list by month, according to the custom date field. I would like to creaet my own custom "Next Month" and "Previous Month" pagination links.
Solution: Add a date filter as described in the related ticket. Then add the following code to your functions.php file:
function monthly_pag_filter_query($query, $view_settings, $view_id ) { $views = array( 562 ); $date_field_slug = 'data'; $defaults = array( 'meta_query'=> array( 'key'=>'wpcf-' . $date_field_slug ) ); if( in_array( $view_id, $views ) ){ $mm = isset($_REQUEST["mm"]) ? $_REQUEST["mm"] : date("m-Y", time()); $mq = isset( $query['meta_query'] ) ? $query['meta_query'] : $defaults; $query['meta_query'] = $mq; foreach($query['meta_query'] as $i => $meta_query) { if (isset($meta_query['key']) && $meta_query['key'] == 'wpcf-' . $date_field_slug ) { $start = strtotime("1-{$mm} 00:00:00"); $numdays = date("t", $start); $end = strtotime("$numdays-{$mm} 23:59:59"); $meta_query['value'] = "$start,$end"; $meta_query['compare'] = 'BETWEEN'; $query['meta_query'][$i] = $meta_query; } } } return $query; } add_filter('wpv_filter_query', 'monthly_pag_filter_query', 12, 3); add_shortcode("next-month", "get_next_month"); function get_next_month($atts) { $mm = isset($_REQUEST["mm"]) ? $_REQUEST["mm"] : date("m-Y", time()); $nextmonth = date("m-Y", strtotime("1-{$mm} +1 month")); return $nextmonth; } add_shortcode("prev-month", "get_prev_month"); function get_prev_month($atts) { $mm = isset($_REQUEST["mm"]) ? $_REQUEST["mm"] : date("m-Y", time()); $prevmonth = date("m-Y", strtotime("1-{$mm} -1 month")); return $prevmonth; }
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 11 replies, has 2 voices.
Last updated by 5 years, 8 months ago.
Assisted by: Christian Cox.