0

I need to solve duplicating of posts and pages caused by unexpected paging. I am trying to disable the paging with this function, but it seems I miss something:

function wpse_disable_pagination( $query ) {
  if( $query->is_single() && $query->is_page() ) {
    $query->set('nopaging', 1 );
  }
}
add_action( 'pre_get_posts', 'wpse_disable_pagination' );
  • change 1 to true 'nopaging' => true – Jignesh Patel May 22 '18 at 10:58
  • It return syntax error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')' – tw8sw8dw8 May 22 '18 at 11:05
  • put your code here i will check it – Jignesh Patel May 22 '18 at 11:07
  • sorry mistake please updated answer – Jignesh Patel May 22 '18 at 11:09
0
function wpse_disable_pagination( $query ) {
  if( $query->is_single() && $query->is_page() ) {
    $query->set( 'nopaging' , true );
  }
}
add_action( 'pre_get_posts', 'wpse_disable_pagination' );

NOTE: nopaging (boolean) - show all posts or use pagination. Default value is 'false', use paging.

  • It doesn't work with double arrow, it bring syntax error. – tw8sw8dw8 May 22 '18 at 11:10
  • yes it's mistake i am update my code – Jignesh Patel May 22 '18 at 11:10
  • you remove pagination on only single post page not in post listing (archive) page? – Jignesh Patel May 22 '18 at 11:13
  • Yes, I am trying to remove pagination only in single posts and pages. – tw8sw8dw8 May 22 '18 at 11:15
  • Yes, only on single posts pages. – tw8sw8dw8 May 22 '18 at 11:19
0

I found the right solution, thanks to Jeff Morris:

global $posts, $numpages;

    $request_uri = $_SERVER['REQUEST_URI'];

    $result = preg_match('%\/(\d)+(\/)?$%', $request_uri, $matches);

    $ordinal = $result ? intval($matches[1]) : FALSE;

    if(is_numeric($ordinal)) {

        // a numbered page was requested: validate it
        // look-ahead: initialises the global $numpages

        setup_postdata($posts[0]); // yes, hack

        $redirect_to = ($ordinal < 2) ? '/': (($ordinal > $numpages) ? "/$numpages/" : FALSE);

        if(is_string($redirect_to)) {

            // we got us a phantom
            $redirect_url = get_option('home') . preg_replace('%'.$matches[0].'%', $redirect_to, $request_uri);

            // if page = 0 or 1, redirect permanently
            if($ordinal < 2) {
                header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently');
            } else {
                header($_SERVER['SERVER_PROTOCOL'] . ' 302 Found');
            }

            header("Location: $redirect_url");
            exit();

        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.