skip to Main Content

I have the following set up in my template file:

<div class="flex" 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 class="blog-entries">
            <?php if (has_post_thumbnail()) { 
            $featuredImg = get_the_post_thumbnail_url();
        } else {
            $featuredImg = get_stylesheet_directory_uri() . '/img/blk-fri.jpg';
        }  ?>

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

        </div>

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

    </div>
    <div class="pagination">
        <?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/

2

Answers


  1. Chosen as BEST ANSWER

    The imported posts from the client were all marked 'sticky' in Wordpress. This was overwriting the post_per_page argument.


  2. 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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search