Home > Net >  Wordpress pagination issue - I set 'posts_per_page' which works for every page except for
Wordpress pagination issue - I set 'posts_per_page' which works for every page except for

Time:12-16

I have the following set up in my template file:

<div  id="allPosts">
        <?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array (
        'posts_per_page'    => 12,
        'paged'             => $paged,
        'order'             => 'DESC',
    );
    $wp_query = new WP_Query( $args ); ?>

        <?php if ( $wp_query->have_posts() ) : ?>
        <?php while ($wp_query -> have_posts()) : $wp_query -> the_post(); ?>
        <div >
            <?php if (has_post_thumbnail()) { 
            $featuredImg = get_the_post_thumbnail_url();
        } else {
            $featuredImg = get_stylesheet_directory_uri() . '/img/blk-fri.jpg';
        }  ?>

            <a  href="<?php the_permalink(); ?>">
                <div  style="background-image: url('<?php echo $featuredImg; ?>');">
                    <div>
                        <div >By <?php the_author(); ?></div>
                        <div >
                            <p><?php echo get_the_date('M'); ?></p>
                            <p><?php echo get_the_date('d'); ?></p>
                        </div>
                    </div>
                </div>
                <div >
                    <h4><?php the_title(); ?></h4>
                    <?php the_excerpt(); ?>
                </div>
            </a>

        </div>

        <?php endwhile; ?>
        <?php endif; ?>

    </div>
    <div >
        <?php 
        echo paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $wp_query->max_num_pages,
            'current'      => max( 1, get_query_var( 'paged' ) ),
            'format'       => '?paged=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
            'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
            'add_args'     => false,
            'add_fragment' => '',
        ) );
    ?>
    </div>

    <?php wp_reset_postdata(); ?>

This functionality is working, except on page one. Every other page limits the amount of posts seen to 12 as specified in the 'posts_per_page', but page 1 is just showing every post. Ive tried searching around for a similar problem, but have come up short. I would appreciate any and all guidance on what might be happening here, thanks!

You can view the issue here: http://listingmirror.devsite.work/blog/

CodePudding user response:

try in that form

    // WP_Query arguments
$args = array(
    'nopaging'               => false,
    'paged'                  => '12',
    'posts_per_page'         => '12',
    'posts_per_archive_page' => '12',
    'order'                  => 'DESC',
    'orderby'                => 'title',
);

// The Query
$paged = new WP_Query( $args );

// The Loop
if ( $paged->have_posts() ) {
    while ( $paged->have_posts() ) {
        $paged->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

CodePudding user response:

Looks like a case of just needing to step away for a day. Looked in all the wrong places. I completely overlooked that the imported posts from the client were all marked 'sticky' in Wordpress. This was overwriting the post_per_page argument. This is resolved now, but I wanted to put the resolution here in case anyone ever runs into the same issue!

  • Related