WordPress pagination links not showing up for custom post type query

I am trying to paginate list of custom post types. This list shows okay, but I can’t seem to make pagination links to show.

Here is the code:

Read More
<?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $args = array(
            'post_type' => 'families',
            'posts_per_page' => 9,
            'paged'=>$paged
        );

        $the_query = new WP_Query( $args );

    ?>

    <table class="families">

        <tr>
            <th class="family_head">Family Head</th>
            <th class="address">Address</th>
            <th class="contact_number">Contact number</th>
        </tr>

        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <tr>

            <td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>

            <td><?php the_field( 'residence_address' ); ?></td>

            <td class="contact_number"><?php the_field( 'mobile_number_1' ); ?></td>

        </tr>

        <?php endwhile; ?>

        <div class="navigation">
            <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
            <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
        </div>

        <?php else: ?>

        <?php endif; ?>

    </table>

Other similar WP functions for pagination aren’t working either. What am I doing wrong?

Related posts

Leave a Reply

2 comments

  1. I erased all the code for pagination and re-did it. It works now. See if you can find the change.

    Here is the working code –

    <div id="main_content">
    
    <div id="main_content_otherpages">
    
        <?php
    
            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
            $args = array(
                'post_type' => 'families',
                'posts_per_page' => 20,
                'paged' => $paged
            );
    
            $the_query = new WP_Query( $args );
    
        ?>
    
        <table class="families">
    
            <tr>
                <th class="family_head">Family Head</th>
                <th class="address">Address</th>
                <th class="contact_number">Contact number</th>
            </tr>
    
            <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
            <tr>
    
                <td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>  
    
                <td><?php the_field( 'residence_address' ); ?></td>
    
                <td class="contact_number"><?php the_field( 'mobile_number_1' ); ?></td>
    
            </tr>
    
            <?php endwhile; ?>
    
        </table>
    
        <div style="clear:none; float:left"><?php next_posts_link( '&larr;Previous', $the_query->max_num_pages ); ?></div>
        <div style="clear:none; float:right"><?php previous_posts_link( 'Next&rarr;' ); ?></div>
    
        <?php wp_reset_postdata(); ?>
    
        <?php else: ?>
    
        <?php endif; ?>
    
    </div>
    

  2. I think that is the problem of get_query_var(‘paged’),Where you are using this code, have checked $paged,
    Try to replace this

    $paged = (get_query_var('page')) ? get_query_var('page') : 1;

    There is a difference between page vs. paged.
    You can read more HERE

    Hope, this will help. You can ask if may i help you further.Thanks