Is there a way to use pagination on a page query like with posts?
Here is the code I’m using:
$listingPages = get_posts(
array(
'cild_of' => '7',
'meta_key' => 'Type',
'post_type' => 'page',
'post_status' => 'publish',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => '20',
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
)
);
I can add /page/2 /page/3/ and etc to the URL and see the next set of 20 however when I add in my pagination code nothing shows up.
<?php wpbeginner_numeric_posts_nav(); ?>
Here is the function.php part of this:
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>â¦</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>â¦</li>' . "n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "n", get_next_posts_link() );
echo '</ul></div>' . "n";
}
You should check your code because you have some sentences that will make your pagination function returns empty value. For example, the next piece of code will get you out because you are in a page template, so is_singular() returns
true
:Also, you are using the global
$wp_query
object inside your pagination function but you are usingget_posts()
to get the list of pages, not the main query. So$wp_query->max_num_pages
is cero because the main query was for a single page. So this piece of code will also get you out:Removing both blocks of code won’t be enough because you use again the global
$wp_query
object to get the max number of pages:Also, if you set the page template as the front page of your blog, the
paged
var won’t work in the query, you should use thepage
query var.I recommend you to think again about what you are trying to do. Looking at your code, you are trying to list the child pages of the page with ID 7 and paginate the results. It seems easier convert that child pages to normal posts under a category and display them in a standard category template.
If you really need the items be child pages, in your case it should be better to use a
new WP_Query()
object, notget_posts()
function and get the max number of pages to your pagination function:And the pagination function:
This looks like a secondary query.
paged
is Core query var— that is, it is used by the WordPress Core. That variable is going to be manipulated by the Core during an ordinary page load by the main query early in the page load. The main query does not know what your query does further down in the page load. What is happening, I am pretty sure, is that you are attempting to use a “reserved” query variable and you are seeing interference/conflict from the main query and the operation of Core functions.The fix, if I am right, is to use a variable other than one used by the Core– something like
my_paged
.