Skip Navigation

[Resolved] Monthly pagination not working properly

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

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 support ticket is created 5 years, 8 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
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 patrizioT 5 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1211313

Tell us what you are trying to do?
I'm using a pagination by month that i found on your support page. But there is something not working.
On first load it's not taking the current month. It will show all the posts.
Is there any documentation that you are following?
https://toolset.com/forums/topic/next-month-and-previous-month-pagination/
Is there a similar example that we can see?

What is the link to your site?
hidden link

#1211435

Can you answer a few questions for me?
- Is your list created as a View or a WordPress Archive? The solution presented here only works for Views because there is no API for archive filters.
- Please edit the View and find the Query Filter section. If you cannot see it, please scroll to the top right corner and click Screen Options to activate the panel. Then open all custom field filter and take a screenshot so I can see the options.
- Have you made any modifications to this code? It's a bit outdated. If not, the code may throw some notices or warnings in PHP. It's also written in a way that will apply this filter to every View on your site. I can help you work around those problems if necessary.

#1211789
Schermata 2019-03-08 alle 09.59.36.png

Hello Christian,
here are my answer:

1. I have created the list as View
2. I will attach a picture for the query filter. I have created a personal field "Data". It's the date of the event i will record.
3. This is the code of the "Search and Pagination" section:

[wpv-filter-start hide="false"]

<div>
<table style="width: 100%">
<tr>
<td style="width: 33%">
[wpv-conditional if="( get_prev_month() >= get_my_this_month() )" ]Precedente[/wpv-conditional]</td>
<td style="text-align: center">
<h4>[sel-month]</h4></td>
<td>
Successivo</td>
</tr>
</table><br>
</div>

[wpv-filter-end]

I have used a conditional option for hide the "prev-month" when is the actual month, because i can't filter anymore my records by date, why i'm using the field already for pagination by month. Maybe there is a solution by coding it?

4. No i didn't make any modifcation. Here is my code:

/* ----------------------------------------------------------------------

Pagination by months

------------------------------------------------------------------------- */

function my_filter_query($query, $view_settings) {
$mm = $_REQUEST["mm"];
if(empty($mm)) {
$mm = date("m-Y", time());
}

if(!empty($mm)) {
if (is_array($query['meta_query'])) {
foreach($query['meta_query'] as $i => $meta_query) {
//print_r($meta_query);

if (isset($meta_query['key']) && $meta_query['key'] == 'wpcf-data') {
$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', 'my_filter_query', 12, 2);

add_shortcode("next-month", "get_next_month");
function get_next_month($atts) {
$mm = $_REQUEST["mm"];

if(empty($mm)) {
$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 = $_REQUEST["mm"];

if(empty($mm)) {
$mm = date("m-Y", time());
}

$prevmonth = date("m-Y", strtotime("1-{$mm} -1 month"));

return $prevmonth;
}

add_shortcode("sel-month", "get_sel_month");
function get_sel_month($atts) {
$mm = $_REQUEST["mm"];

if(empty($mm)) {
$month = date("m", time());
$month=month_italian($month);
$mm = $month." ".date("Y", time());
}else {
$date=date_create_from_format("m-Y","$mm");
$date_month=date_format($date,"m");
$date_month=month_italian($date_month);
$mm=$date_month." ".date_format($date,"Y");

}

return $mm;
}

function month_italian($a){

switch ($a){
case "01":
$month="Gennaio";
break;
case "02":
$month="Febbraio";
break;
case "03":
$month="Marzo";
break;
case "04":
$month="Aprile";
break;
case "05":
$month="Maggio";
break;
case "06":
$month="Giugno";
break;
case "07":
$month="Luglio";
break;
case "08":
$month="Agosto";
break;
case "09":
$month="Settembre";
break;
case "10":
$month="Ottobre";
break;
case "11":
$month="Novembre";
break;
case "12":
$month="Dicembre";
break;
}

return $month;

}

function get_my_this_month($atts) {

$my_this_month = date("m-Y", time());

return $my_this_month;
}

I hope you can help me figure out where is the problem.

THX

#1212637

I think you will appreciate this function in PHP: http://php.net/manual/en/function.strftime.php

It will format a date according to your locale settings, so Italian month names will be translated automatically without the need for additional sel_month formatting or month_italian 🙂

Next, please replace your filter code, next-month and prev-month shortcodes with this update:

function monthly_pag_filter_query($query, $view_settings, $view_id ) {
    $views = array( 562 );
    $date_field_slug = 'data';
    if( in_array( $view_id, $views ) ){
      $mm = isset($_REQUEST["mm"]) ? $_REQUEST["mm"] : date("m-Y", time());

      $mq = isset( $query['meta_query'] ) ? $query['meta_query'] : array();
      $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;
}

If this isn't working as expected, I'll need to log in to your wp-admin area and take a closer look.

#1213608

Hello Christian,
just put this new code, but the problem still there.

#1213710

It's safe to write here credentials for access our wp-admin area?

#1213728

Here are some private reply fields where you can share admin login credentials.

#1213766

I'm working on this now, please stand by and I'll update you shortly.

#1213782

Okay I made a small adjustment to the code in your child theme's functions.php file. Please be sure to download that change into your local file repository so it's not overwritten. Here is the filter code I placed:

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);

Please take a look and let me know if it is not working as expected.

#1213819

Thx a lot. It works fine!!

I have another question, it's possible to use this code for paginate/filtering the backend view? There is a way on ToolSet to set filter option in admin? I can't found it. THX

#1213820

No, there's nothing built-in to Toolset that will help you modify the pagination of admin lists like this. You can sort by the field value, but dynamic pagination isn't offered.

#1214559

My issue is resolved now. Thank you!