WordPress issue with stick posts

I’m using sticky posts to allow certain posts to be pinned in a featured post area. I have it working on a development server but when I moved it to the live server it only partially works. If there is a sticky post it displays it. But if there isn’t a sticky post it displays nothing and it should display the most recent post. Is there an alternate way to handle this that may work?

$options = array(
    'post_type' => post,
    'posts_per_page' => 1,
    'post__in'  => get_option( 'sticky_posts' ),
    'ignore_sticky_posts' => 1,
    'status' => 'publish'
);

Related posts

1 comment

  1. You can check result of get_option( 'sticky_posts' ) first:

    $sticky = get_option('sticky_posts');
    if ( !empty($sticky) ) {
       // query options for sticky posts
       $options = array(
           'post_type' => post,
           'posts_per_page' => 1,
           'post__in'  => $sticky,
           'ignore_sticky_posts' => 1,
           'status' => 'publish'
       );
    } else {
        // query options for the most recent post
        $options = array(
            'posts_per_page' => 1,
            'paged' => 1,
            'orderby' => 'post_date',
            'order' => 'DESC',
            'post_status' => 'publish'
        );
    }
    

Comments are closed.