1

I have on my page section with randomly selected posts via new WP_Query. The problem is that 'posts_per_page' attribute don't work. Here is my code:

<div id="featured">
    <?php 

        $args = array(
            'post_type' => 'post',
            'orderby'   => 'rand',
            'posts_per_page' => 4,
            'nopaging' => true,
            );

        $the_query = new WP_Query( $args );
        if ( $the_query->have_posts() ) {

            echo '<div style="table full">';

            while ( $the_query->have_posts() ) {

                $the_query->the_post(); 

            ?>

                <div class="featcell" style="background: url(<?php the_post_thumbnail_url(); ?>) center center">
                    <a class="featartlink" href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a>
                </div>

            <?php 

            }

            echo '</div>';

            wp_reset_postdata();

        } 

    ?>
</div>

The result of script above is that script loading all posts from database. Script is placed under post on single post page. What i doing wrong? It appears that all is OK, but it is not! Thank You for help.

3

nopaging disables pagination, and posts_per_page is a pagination parameter. You are telling it to ignore pagination and return all posts.

  • Did You trying to use this code to check your answer tell the true? Unfortunately, neither remove or set the value to false do not change anything. Of course Your answear is very logic, but i trying before use this code without 'nopaging' attribute and result was same... posts_per_page do not work. – X9DESIGN Mar 19 '17 at 22:14
  • If inspecting the query shows that the posts_per_page parameter is ignored, then the likely issue is that there is errant code somewhere in a plugin or your theme that is modifying the query. It makes sense that this is the case if get_posts works but WP_Query doesn't, because get_posts suppresses those query filters by default. get_posts uses WP_Query internally, so if identical parameters for both functions return different results, you have some bad code lurking somewhere. – Milo Mar 28 '17 at 1:57
1

I dont why, but after some tests i trying to use also get_posts() function and all is works fine now. I just wonder why new WP_Query don't want to work.

Here is the correct code with the use get_posts() function.

<div id="featured">

    <?php 

        global $post;

        $args = array( 

            'post_type' => 'post', 
            'posts_per_page' => 4, 
            'orderby' => 'rand',

        );

        $rand_posts = get_posts( $args );
        if ( $rand_posts ) : 

            echo '<div style="table full">';

            foreach ( $rand_posts as $post ) : setup_postdata( $post ); ?>

                <div class="featcell" style="background: url(<?php the_post_thumbnail_url(); ?>) center center">
                        <a class="featartlink" href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a>
                </div>

            <?php endforeach; wp_reset_postdata(); 

            echo '</div>';

        endif;

    ?>

</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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