10

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.

1

1 Answer 1

18

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:

<?php
    $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() ) {
            $your_query->the_post();
            // do something
        }
    } else {
        echo 'Sorry, no posts found.';
    }
?>
3
  • 1
    Thanks very much for that, interesting reading. However please could you provide the alternative using the get_posts() method. Is it as simple as: <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; get_posts("post_type=videos&posts_per_page=9&paged=$paged"); ?> That isn't actually working for me but that might be because of code elsewhere?
    – Johnny
    Jul 10, 2013 at 14:13
  • 1
    See updated answer. Jul 10, 2013 at 15:58
  • 2
    Thank you very much for taking the time out to show me how to use this. I really appreciate it.
    – Johnny
    Jul 10, 2013 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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