WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP_Query and limit post (7 posts)

  1. Franz Ordˇ˝ez
    Member
    Posted 11 months ago #

    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?

  2. Paul Kaiser
    Member
    Posted 11 months ago #

    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

  3. Franz Ordˇ˝ez
    Member
    Posted 11 months ago #

    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:

  4. tiaanswart
    Member
    Posted 11 months ago #

    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();
    
    ?>
  5. Franz Ordˇ˝ez
    Member
    Posted 11 months ago #

    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."

  6. Franz Ordˇ˝ez
    Member
    Posted 11 months ago #

    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 :)

  7. tiaanswart
    Member
    Posted 11 months ago #

    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?

Reply

You must log in to post.

About this Topic