[Resolved] Help to Get WP-PageNavi Working for Page 2 Onwards

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.

This topic contains 10 replies, has 2 voices.

Last updated by patL-3 4 years, 10 months ago.

Assigned support staff: Minesh.

Author
Posts
#305097

Hi,

With the help of these 2 threads, i've got WP-PageNavi to show up in the right place and look perfect:

https://toolset.com/forums/topic/url-rewriting-for-pagination/
https://toolset.com/forums/topic/how-to-put-the-pagination-from-wp-pagenavi-at-the-bottom/

Q1: The only problem is that it doesn't work for Page 2 and beyond (ie the content doesn't change and the pagination controls stay the same too). Seems like this is a common problem - https://wordpress.org/support/topic/wp-pagenavi-version-272-not-showing-page-2

The plugin author suggests a solution, but i'm not sure how to implement it - hidden link

This is the code i'm using in functions.php

function pagenavi_paged($q) {
  $types = (array)$q->get('post_type');
  if (in_array('test', $types)) {
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $q->set('paged', $paged);
  }
}
add_action('pre_get_posts', 'pagenavi_paged');
  
// Replace wpv-pagination shortcode
function wpv_pagenavi($args, $content) {
  global $WP_Views;
  return wp_pagenavi( array('query' => $WP_Views->post_query, 'echo'=>false) );
}
add_shortcode('wpv-pagination', 'wpv_pagenavi');

Q2: On a slightly different topic ... when the code was initially shared, Caridad mentioned that 'test' (from if (in_array('test', $types))) should be changed to the name of whatever CPT is being used. But it doesn't seem to make a difference whether i leave it on 'test' or change it to my CPT's name. Should it be like this, or is something not quite right?

Thanks for your assistance.

#305202

Minesh
Supporter

Languages: English (English )

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

Hello and thank you for contacting Toolset support:

Did you try to use 'page' as query variable. you can also try passing the ‘page’ query var:

query_posts( array( 'cat' => 8, 'paged' => get_query_var('page') ) );

OR

Please try to check your issue by replacing your 'pagenavi_paged' function as given under: Try to check with both 'paged' and 'page' query variable.

function pagenavi_paged($q) {
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $q->set('paged', $paged);
}
#305419

Hi Minesh,

Sorry, i don't quite follow what you're saying. The code i'm using was shared by a TVC Supporter and it seemed to work for other users. So why should i "try to use 'page' as query variable"?

Similarly with your second suggestions, i can't really follow. Appreciate if you elaborate in a little bit more detail please.

Thanks

#305493

Minesh
Supporter

Languages: English (English )

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

I suggest you because I found that hint on the link you provided [ check last two lines ]:
=> hidden link

Last two lines are:

If that doesn't work, you can also try passing the 'page' query var:
query_posts( array( 'cat' => 8, 'paged' => get_query_var('page') ) );

OR

Try to add following code with your functions.php file and check your issue.

function pagenavi_paged($q) {
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $q->set('paged', $paged);
}
add_action('pre_get_posts', 'pagenavi_paged');
   
// Replace wpv-pagination shortcode
function wpv_pagenavi($args, $content) {
  global $WP_Views;
  return wp_pagenavi( array('query' => $WP_Views->post_query, 'echo'=>false) );
}
add_shortcode('wpv-pagination', 'wpv_pagenavi');
#305500

Thanks for your prompt response Minesh.

Tried your second suggestion with this code, and it worked:

function pagenavi_paged($q) {
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $q->set('paged', $paged);
}
add_action('pre_get_posts', 'pagenavi_paged');
    
// Replace wpv-pagination shortcode
function wpv_pagenavi($args, $content) {
  global $WP_Views;
  return wp_pagenavi( array('query' => $WP_Views->post_query, 'echo'=>false) );
}
add_shortcode('wpv-pagination', 'wpv_pagenavi');

Looks like all we did was to remove these 2 lines:

$types = (array)$q->get('post_type');
  if (in_array('test', $types)) {

Do you know why this made all the difference?

#305553

Minesh
Supporter

Languages: English (English )

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

Yes, you are right, We have remove the if condition that was used to check the post type.

#305638

"Yes, you are right, We have remove the if condition that was used to check the post type."

Do you know why that made all the difference? Presumably, that was working before, as other users had implemented it without a problem.

#305950

Minesh
Supporter

Languages: English (English )

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

As you can see in following code, there is a if condition that checks your post type. If you want to check for specific post type you need to replace your post type with 'test'.

$types = (array)$q->get('post_type');
  if (in_array('test', $types)) {

For example, lets say your post type is books. Your condition should look like:

$types = (array)$q->get('post_type');
  if (in_array('books', $types)) {

What we have done is we have removed the if condition and the code now is universal for all post types.

#306024

Oh yes, that makes sense Minesh.

In my original post, you can see i posted: "Q2: On a slightly different topic ... when the code was initially shared, Caridad mentioned that 'test' (from if (in_array('test', $types))) should be changed to the name of whatever CPT is being used. But it doesn't seem to make a difference whether i leave it on 'test' or change it to my CPT's name. Should it be like this, or is something not quite right?"

To clarify ... I did change 'test' to the name of my CPT and ...

a) The numbered pagination feature did appear but ...
b) It only worked for the first page and had errors from page 2 onwards (as we have solved in this thread by removing those 2 lines).

So, while i understand how those 2 lines of code use an IF condition to check for a post type, the real question is ... why does PageNavi only work properly from page 2 onwards when that IF condition is removed.

#306049

Minesh
Supporter

Languages: English (English )

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

For that I need to debug further. Could you please send me link where I should check this issue with pageNavi.

*** Please take FULL BACKUP of your database and your website.***
In order to investigate your issue if we require to check things on your install in order to see what could be going wrong there.

If you agree to this please use the form fields I have enabled below this comment box to provide temporary access details (wp-admin and FTP)?

I have set next reply as private.

#306254

Hi Minesh,

I have changed the code per the suggestions in this thread and it's all working now. In order for you to troubleshoot the original issue, i would need to change it back and then wait for you to take a look. But i'm a little pressed for time and need to just move on for now. If you could see something wrong with the original code paste above, that would be good, otherwise, there's no need to debug further.

Thanks a lot for your help.