Search code examples
wordpresscodex

how to limit the number posts per page


In creating my theme, I want to show one sticky post in a loop but unfortunately, all sticky posts (there are 5) are displaying. I just want to show 1 or two but I am unable to do so through my coding. I don't know what I am missing or what I am doing wrong.

<?php
       $query = new WP_Query(array(
       'post_per_page'    => 1,
       'post__in'          => get_option('sticky_posts'),
        'paged'             => $paged,
                        
                    ));
?>


Solution

  • To get the last sticky post:

    $sticky = get_option( 'sticky_posts' );
    $args = array(
        'posts_per_page' => 1,
        'post__in'  => $sticky,
        'ignore_sticky_posts' => 1
    );
    query_posts( $args );
    if ( $sticky[0] ) {
        // insert here your stuff...
    }