Adding the “paged” parameter to a query

Vu Nam Hung
Vu Nam Hung
Published in
1 min readOct 31, 2016

If WP_Query is altering the main loop and the “paged” parameter is not set you’ll need to add it with get_query_var(). This is so WordPress knows exactly what page it’s on.

For example, if your query looks like this (without the “paged” parameter):

<?php $the_query = new WP_Query( 'posts_per_page=3' ); ?>

you add the parameter like this:

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query( 'posts_per_page=3&paged=' . $paged );
?>

The next example is exactly the same as above but with the parameters in an array:

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query( $args );
?>

--

--