Query sticky posts only in WordPress

I have the query below to gather the sticky posts. This works when i actually have sticky posts, but when i have no sticky posts it just queries 10 (none sticky) posts. How can i adjust the query to gather just and only sticky posts and if none are found, just do nothing. Thanks

/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );

/* Query sticky posts */
$the_query2 = new WP_Query( array( 'ignore_sticky_posts' => 'false', 'post__in' => $sticky, 'post_type' => 'ad_listing' ) );
echo $the_query2->post_count;

Related posts

1 comment

  1. Found out that by adding this if statement, only sticky posts are queried!

    /* Get all sticky posts */
    $sticky = get_option( 'sticky_posts' );
    
        if (!empty($sticky)) {
        //Do the query etc.
        }
    

Comments are closed.