• I have the following code from a WP site that a client give to me already set.

    <?php

    $featured_args = array(
    ‘post__in’ => get_option( ‘sticky_posts’ ),
    );

    $featured_loop = new WP_Query( $featured_args );

    while( $featured_loop->have_posts() ) :
    $featured_loop->the_post();

    ?>

    Problem is, I just want to show the latest 5 sticky posts, and no more. Already tried methods here on forums to resolve it, but none worked. ¿Can someone help me?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Howdy,
    Try adding another parameter to featured_args:

    $featured_args = array(
    'posts_per_page' => 5,
    'post__in' => get_option( 'sticky_posts' ),
    );

    (Keep the rest of your code the same, then…)

    Good luck,
    Paul

    Thread Starter Franz Ordóñez

    (@franzbleu)

    Hello! I already did it, so another methods suggested in general way, it keeps taking the whole amount of sticky posts.

    That’s the reason because I wrote you the code, i’m trying to find out what’s going on here D:

    you need to reduce the amount of sticky posts before you include it in the query args like so:

    <?php
    
    $sticky = get_option( 'sticky_posts' );
    
    if ($sticky) {
        $sticky = array_slice($sticky, 0, 5)
    }
    
    $featured_args = array(
        'post__in' => $sticky,
    );
    
    $featured_loop = new WP_Query( $featured_args );
    
    while( $featured_loop->have_posts() ) :
    $featured_loop->the_post();
    
    ?>
    Thread Starter Franz Ordóñez

    (@franzbleu)

    Thanks for your answer tiaanswart, but doesn’t work, when I do that, then reload, thows me “HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.”

    Thread Starter Franz Ordóñez

    (@franzbleu)

    Finally! I figured it out:

    <?php

    $featured_args = array(
    ‘post__in’ => get_option( ‘sticky_posts’ ),
    );

    $featured_loop = new WP_Query( $featured_args );

    $counter = 1;

    while( $featured_loop->have_posts() && $counter < 5 ) :
    $featured_loop->the_post();

    ?>

    (The post loop…)

    <?php $counter++; endwhile; wp_reset_postdata(); ?>

    I’ve tried things really close, but my mistake was use initially a var $i, as I thought it’s a too common name, so I named it $counter and problem solved! Thanks all for the help 🙂

    My apologies my code was missing a ; on the end of line 5.

    The code should look like this:

    $sticky = get_option( 'sticky_posts' );
    
    if ($sticky) {
        $sticky = array_slice($sticky, 0, 5);
    }
    
    $featured_args = array(
        'post__in' => $sticky,
    );
    
    $featured_loop = new WP_Query( $featured_args );
    
    while( $featured_loop->have_posts() ) :
    $featured_loop->the_post();

    Your solution is correct however your query is doing some unnecessary work getting all the posts and then only using some of it. Won’t it rather make sense querying the correct (amount of) data?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WP_Query and limit post’ is closed to new replies.