Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Another user on here suggested that my use of queries was incorrect so I'm changing the code. However, the pagination in the first block of code (below) isn't showing up in the HTML while the second one (my original) shows up fine.

This one doesn't work (new code):

                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    $loop = new WP_Query( array(
                        'post_type' => 'movies',
                        'paged' => $paged,
                        'posts_per_page' => 6
                    ));

                    if($loop->have_posts()){
                        while ( $loop->have_posts() ) {
                            $loop->the_post();

                            << LOOP GOES HERE >>


                   <?php
                      }
                   }
                   my_paginate_links();
                   wp_reset_postdata();
                   ?>

This one works (my original code):

                    $c=0;
                    $i=1;

                    $temp = $wp_query;
                    $wp_query = null;
                    $wp_query = new WP_Query();
                    $wp_query->query('post_type=movies' . '&paged=' . $paged . '&posts_per_page=6');

                    while ( $wp_query->have_posts() ) : $wp_query->the_post(); $c++;


                 << LOOP GOES HERE >>

                <?php
                    endwhile; 
                    my_paginate_links();
                    $wp_query = null;
                    $wp_query = $temp;
                    wp_reset_query();
                ?>

How can I get pagination to show up in the first block of code?

share|improve this question
    
For getting the paged parameter use this <?php $paged = (get_query_var('page')) ? get_query_var('page') : 1; ?> –  Rohit Pande Jan 14 '13 at 11:14
    
I replaced the line but the pagination still doesn't show, unfortunately. –  Desi Jan 14 '13 at 11:16
    
Have you considered using a custom post type archive instead of reinventing the wheel and building your own? ( e.g. above ) –  Tom J Nowell Jan 14 '13 at 13:50
    
Also +1 on the much improved use of WP_Query from your recent question here: wordpress.stackexchange.com/questions/81475/… You're on the right track =D –  Tom J Nowell Jan 14 '13 at 13:51
add comment

2 Answers 2

up vote 1 down vote accepted

Rather than attempt to fix your query, I recommend you sidestep it entirely by using custom post type archives!

With permalinks on, you should be able to view a list of all your movies posts by going to:

example.com/movies

Then, in your theme, create archive-movies.php, and it will be used instead of the archive.php/index.php for movies archives. Make sure to use the default loop not a custom loop, and WordPress will take care of the post type and pagination in your query.

To control how many posts are displayed on this archive, you can do a filter on pre_get_posts to modify the main query before it happens.

function john_movies_pagesize( $query ) {
    // exit out if it's the admin or it isn't the main query
    if ( is_admin() || ! $query->is_main_query() )
        return;
    // so its not admin, and its the main query, is it the movies post archive?
    if ( is_post_type_archive( 'movies' ) ) {
        // it is!! Set the posts_per_page to 6
        $query->set( 'posts_per_page', 6 );
        return;
    }
}
add_action( 'pre_get_posts', 'john_movies_pagesize', 1 );
share|improve this answer
    
Thanks, Tom. This seems like a more efficient method. By the way, do you know what may be wrong with the original question just for my future reference? Thanks again! –  Desi Jan 14 '13 at 14:17
    
your pagination function ( which we cant see so I can only speculate ) is probably calling the default function calls, which refer to the main query ( $wp_query ) not your custom query, and so will generate incorrect page numbers and counts –  Tom J Nowell Jan 14 '13 at 15:31
add comment

Here is a working example of how to use paged pagination with custom post types:

<?php
$paged = ( get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'newsfeed', 'posts_per_page' => 5, 'paged' => $paged );
$newsquery = new WP_Query( $args );
if ( $newsquery->have_posts() ) :
while ( $newsquery->have_posts() ) : $newsquery->the_post();

the_excerpt(); //query here

global $newsquery; $total_pages = $newsquery->max_num_pages; if ( $total_pages > 1 ) {
$big = 999999999; // need an unlikely integer

echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_text' => __(' Prev'),
'next_text' => __('Next '),
'current' => max( 1, get_query_var('paged') ),
'total' => $newsquery->max_num_pages
) );

}
endif; wp_reset_postdata();
?>
share|improve this answer
    
Please add an explanation to your answer: why could that solve the problem? –  toscho Aug 5 '13 at 9:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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