1

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,
                        
                    ));
?>
Jeeshan
  • 65
  • 6
  • I also tried to remove sticky posts from the loop, using 'post__not_in' => get_option('sticky_posts'), then general flow of loop worked and display 1 post as per query.... but when again in included only sticky post . It didn't work. – Jeeshan Sep 08 '19 at 11:22
  • You have a typo there: it's `posts_per_page`, not `post_per_page`. – cabrerahector Sep 08 '19 at 11:55
  • still not working. – Jeeshan Sep 08 '19 at 12:24
  • Now I deleted all sticky post and make a new sticky post. Now it has become more strange..... My latest sticky post is not visible... Nothing is visible whole container is empty. Totally messed up. – Jeeshan Sep 08 '19 at 12:29

1 Answers1

2

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...
}
ManUtopiK
  • 4,495
  • 3
  • 38
  • 52