Support » Everything else WordPress » Exclude Sticky Posts from Query

  • Hi, I’ve been searching for a week a solution to my problem and I didn’t find any, so I hope maybe I could find some help here.

    I want to exclude all sticky posts from my query_posts, which means even if a sticky post should appear in that list because it’s a recent post from this category, I want it (them, the sticky posts) not to show at all.

    Here what I tried so far, without much success:

    <?php array(“post__not_in” =>get_option(“sticky_posts”));
    query_posts(‘showposts=10&cat=2’); ?>

    <?php while (have_posts()) : the_post(); ?>
    My stuff here
    <?php endwhile; ?>

    Any idea how to do that? I thought it could be simple, but it’s clearly not (for me)…

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Something like:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1
    $args = array(
    'category__in' => array(2),
    'posts_per_page' => 10,
    'post__not_in' => get_option('sticky_posts'),
    'paged' => $paged,
    'caller_get_posts'=>1
    );
    query_posts($args);
    <?php while (have_posts()) : the_post(); ?>

    See query_posts() for more arguments.

    Hi MichaelH, thanks! I looked at the Codex during my search but I just want to list 10 posts from category 2 (with a different layout for the first post) so I’m not interested by the paged layout at all.

    And I have no idea how the ‘clean’ this code so it can work with my query_posts…

    Thanks for your help!

    You could have used code, but to make you happier…

    Get 10 posts in category 2, exclude sticky posts.

    <?php
    $args = array(
    'category__in' => array(2),
    'showposts' => 10,
    'post__not_in' => get_option('sticky_posts'),
    'caller_get_posts'=>1
    );
    
    <?php array("post__not_in" =>get_option("sticky_posts"));
    query_posts('showposts=10&cat=2'); ?>
    
    <?php while (have_posts()) : the_post(); ?>
    //My stuff here
    <?php endwhile; ?>

    Thanks a lot!

    I got a Parse error: syntax error, unexpected ‘<‘ so I can’t see if it actually works…

    Ok, a ?> was missing, got it 🙂

    But the sticky post still appears in the list… I changed the category ID but it’s still there. I think it’s because the sticky post is listed as a recent post posted in this category. Which I don’t want because it’s already on my top page so I don’t want a duplicate in the list…

    Here’s my code:

    <?php
    $args = array(
    ‘category__in’ => array(14),
    ‘showposts’ => 10,
    ‘post__not_in’ => get_option(‘sticky_posts’),
    ‘caller_get_posts’=>1
    ); ?>

    <?php array(“post__not_in” =>get_option(“sticky_posts”));
    query_posts(‘showposts=10&cat=14’); ?>

    <?php $i=1; ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php if ($i == 1) : ?>
    //My stuff
    <?php else : ?>
    //My stuff
    <?php endif; ?>
    <?php $i++; ?>
    <?php endwhile; ?>

    You aren’t using the $args, so will give you this one last example:

    <?php
    $args = array(
    'category__in' => array(14),
    'showposts' => 10,
    'post__not_in' => get_option('sticky_posts'),
    'caller_get_posts'=>1
    );
    
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '10 recent Posts for Category 14, excludes sticky posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><small><?php the_time('m.d.y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Good Luck.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Exclude Sticky Posts from Query’ is closed to new replies.