WordPress Pagination Not Working – Always Showing First Pages Content

The question:

I am working on a website where I have a blog but I also have a custom post type to allow me to drop in some videos.

I would like to use pagination so that if there are more then 9 videos displayed, then pagination occurs.

The first part of this works. The videos are indeed limited to 9 per page and the pagination correctly shows up at the bottom.

However when I click on the link for the second page, even though the URL changes, the first pages videos are displayed.

For my ‘normal’ blog posts, pagination is working exactly as intended.

This is the current code that I am using for my custom post type:

<?php if ( have_posts() ) : ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts('post_type=videos&posts_per_page=9&paged=$paged'); ?>

    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>

Any help would be greatly appreciated.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Why your current code fails

Your always getting the content of the first page, because the string of parameters passed to query_posts being encapsulated in single quotes prevents variables (as well as escape sequences for special characters other than $) to be expanded.

query_posts("post_type=videos&posts_per_page=9&paged=$paged"); would take care of that problem.

query_posts('post_type=videos&posts_per_page=9&paged='.$paged); would also.

And finally, passing an array of mixed arguments instead of a URL-query-style string would as well.

That being said though, you should not use query_posts at all:

How it should be done

As per your comment, you attempted to use get_posts.
That is a very useful function indeed, but not the correct way to go if you want to use a WordPress-Loop thereafter.
get_posts returns an array of post objects – the WP_Query object’s posts property. But only that one property, without all the other goodies and methods, that WP_Query provides.

Hence, to go with your above code snippet, do something along the lines of this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    $args = array(
        'post_type' => 'videos',
        'posts_per_page' => 9,
        'paged' => $paged
    $your_query = new WP_Query( $args );

    if ( $your_query->have_posts() ) {
        /* The Loop */
        while ( $your_query->have_posts() ) {
            // do something
    } else {
        echo 'Sorry, no posts found.';

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment