0

I have this code to display only the latest sticky post on the front page.

the strange thing is that, when I'm logged-in it shows non-sticky latest post, even if it's private...

<?php get_header(); ?>
<?php if ( is_home() && !is_paged() ) : ?>

<?php
    // Get IDs of sticky posts
    $sticky = get_option( 'sticky_posts' );
    // first loop to display only my single, 
    // MOST RECENT sticky post
    $most_recent_sticky_post = new WP_Query( array( 
    // Only sticky posts
    'post__in'            => $sticky, 
    // Treat them as sticky posts
    'ignore_sticky_posts' => 1, 
    // Order by date to get the most recently published sticky post
    'orderby'             => date, 
    // Get only the one most recent
    'posts_per_page'      => 1
    ) );
?>

<?php while ( $most_recent_sticky_post->have_posts() ) :  $most_recent_sticky_post->the_post(); ?>
<!-- your code to display most recent sticky -->

<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>

The idea is that those logged-in see the exact same thing as those not logged-in.

Does anyone know how to present that?

1 Answer 1

2

If you want both logged in and non-logged in user to see the same thing then you only want 'published' post status, at least it doesn't make sense to me to show 'private' posts to non-logged in users. Alter you query to include that condition.

$most_recent_sticky_post = new WP_Query( 
  array( 
    'post_status' => array( 'publish' ),
    // Only sticky posts
    'post__in'            => $sticky, 
    // Treat them as sticky posts
    'ignore_sticky_posts' => 1, 
    // Order by date to get the most recently published sticky post
    'orderby'             => date, 
    // Get only the one most recent
    'posts_per_page'      => 1
    ) 
);

ignore_sticky_posts, when true as you have it, treats sticky posts like ordinary posts, which is not what your code comment says but I think that is what you mean.

2
  • Hmmm, that actually didn't work: I get http Error 500. So "'ignore_sticky_posts' => 1," means true?
    – Django
    Commented Feb 17, 2013 at 4:30
  • Try again. There was a syntax error. Yes, 1 will evaluate to true.
    – s_ha_dum
    Commented Feb 17, 2013 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.