Skip Navigation

[Resolved] Previous Next Month pagination almost working

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

Problem:

I have been testing out the custom PHP codes:
https://toolset.com/forums/topic/previous-next-month-pagination-almost-working/#post-1201285

Only issue is that on initial page load it shows posts from all months and not only the ones from the current month. From what I read in Waqas final reply it seems the updated code in his post should support current month on intial view?

Solution:

There are some PHP errors in the custom PHP codes, I suggest debug the codes line by line manually.

Relevant Documentation:

This support ticket is created 5 years, 11 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 10 replies, has 3 voices.

Last updated by bobA-2 5 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1201285

I have been testing out the code one of your supporters provided here:
https://toolset.com/forums/topic/next-month-and-previous-month-pagination/

Does exactly what I need and I have it working here hidden link

Only issue is that on initial page load it shows posts from all months and not only the ones from the current month. From what I read in Waqas final reply it seems the updated code in his post should support current month on intial view?

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-release-date') {
                    $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);
#1201550

Hello,

There are some issues in the thread you mentioned above, please try these:
1) Edit the post view of your website, for example the post view's ID is 123,
in section "Query Filter", change the filter to:
Select items with field:
release-date is a string equal to 03-2019

We will use below filter to change the month value

2) Edit your PHP codes, replace these line from:

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

To:

	if($view_settings['view_id'] != 123){ //if it is specific view
		 return $query;
	}
	$mm = date("m-Y", time()); // current month by default
	if(isset($_REQUEST["mm"])){ // if it is pagination URL
		$mm = $_REQUEST["mm"];
	}

Please replace 123 with your view's ID.

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

#1202408

Thanks for your help Luo. Got it working.

If I may ask. Is it easy to make a Previous Next day pagination by modifying the same code or is that a much more difficult task than by month?

#1202432

Minesh
Supporter

Languages: English (English )

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

Luo is on holiday today. He will get in touch with you when he will get back to work tomorrow.

#1202761

Yes, I think so, there are same issues in Previous Next day pagination shortcode, you can try to replace these lines from:

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

To:


$mm = date("m-Y", time()); // current month by default
if(isset($_REQUEST["mm"])){ // if it is pagination URL
    $mm = $_REQUEST["mm"];
}

#1203835

Thanks Luo,

I think I will contact one of your listed contractors.

I modified the previous / next month code to this for previous / next day but not working.

function my_daysfilter_query($query, $view_settings, $view_id) {

if($view_settings['view_id'] != 39){ //if it is specific view
     return $query;
}
$dd = date("d-m-Y", time()); // current day by default
if(isset($_REQUEST["dd"])){ // if it is pagination URL
    $dd = $_REQUEST["dd"];
}
   
    if(!empty($dd)) {
        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-start') {
                    $start = strtotime("1-{$dd} 00:00:00");
                    $numdays = date("t", $start);
                    $end = strtotime("$numdays-{$dd} 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_daysfilter_query', 12, 2);

add_shortcode("next-day", "get_next_day");
function get_next_day($atts) {
    $dd = $_REQUEST["dd"];
     
    if(empty($dd)) {
        $dd = date("d-m-Y", time());
    }
     
    $nextday = date("d-m-Y", strtotime("1-{$dd} +1 day"));
     
    return $nextday;
}
 
add_shortcode("prev-day", "get_prev_day");
function get_prev_day($atts) {
    $dd = $_REQUEST["dd"];
     
    if(empty($dd)) {
        $dd = date("d-m-Y", time());
    }
     
    $prevday = date("d-m-Y", strtotime("1-{$dd} -1 day"));
     
    return $prevday;
}

#1204166

Yes, according to our support policy, we don't provide custom codes support, you can also check it with our Toolset Contractors:
https://toolset.com/contractors/

#1208578

Could you please help regarding the code you provided in matter of errors in my error.log file?

I get a lot of these

PHP Warning: Missing argument 3 for my_filter_query(), called in /home/public_html/wp-includes/class-wp-hook.php on line 288 and defined in /home/public_html/wp-content/themes/wp-clear2017/functions.php on line 100

This is what I have in the functions file

function my_filter_query($query, $view_settings, $view_id) {

if($view_settings['view_id'] != 74955){ //if it is specific view
     return $query;
}
$mm = date("m-Y", time()); // current month by default
if(isset($_REQUEST["mm"])){ // if it is pagination URL
    $mm = $_REQUEST["mm"];
}
   
    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-program-tidspunkt') {
                    $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;
}
#1209191

Please try to modify this line from:

add_filter('wpv_filter_query', 'my_filter_query', 12, 2);

To:

add_filter('wpv_filter_query', 'my_filter_query', 12, 3);

And test again.

More help:
https://developer.wordpress.org/reference/functions/add_filter/
$accepted_args
(int) (Optional) The number of arguments the function accepts.

#1209224

My issue is resolved now. Thank you!

#1315447

Had some time to go through the code so I made a working prev / next day pagination from the example now in case anybody can use it.

function my_dayfilter_query($query, $view_settings, $view_id) {
 
if($view_settings['view_id'] != 50){ //if it is specific view
     return $query;
}
$mm = date("d-m-Y", time()); // current date by default
if(isset($_REQUEST["mm"])){ // if it is pagination URL
    $mm = $_REQUEST["mm"];
}
    
    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-start') {
                    $start = strtotime("{$mm} 00:00:00");
                    $end = strtotime("{$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_dayfilter_query', 12, 3);
 
add_shortcode("next-day", "get_next_day");
function get_next_day($atts) {
    $mm = $_REQUEST["mm"];
      
    if(empty($mm)) {
        $mm = date("d-m-Y", time());
    }
      
    $nextday = date("d-m-Y", strtotime("{$mm} +1 day"));
      
    return $nextday;
}
  
add_shortcode("prev-day", "get_prev_day");
function get_prev_day($atts) {
    $mm = $_REQUEST["mm"];
      
    if(empty($mm)) {
        $mm = date("d-m-Y", time());
    }
      
    $prevday = date("d-m-Y", strtotime("{$mm} -1 day"));
      
    return $prevday;
}