Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

show just 4 sticky posts WordPress

  • SOLVED

I have a normal home page with no dynamic content, but a loop to shows sticky posts above the normal loop
<blockquote><div id="slider">

<strong><?php query_posts(array('post__in'=>get_option('sticky_posts'), 'showposts' => 4 )); ?></strong>

<?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>

<div class="<?php hybrid_entry_class( 'feature' ); ?>">

<?php get_the_image( array( 'custom_key' => array( 'feature' ), 'default_size' => 'medium', 'width' => '200', 'height' => '200', 'image_class' => 'feature' ) ); ?>

<div class="text-box">
<h2 class="slider-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?>
</a></h2>

<div class="entry-summary entry">
<!-- <?php the_excerpt(); ?> -->
<a class="more-link" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php _e('Full Story &raquo;', 'news'); ?></a>

</div>
</div>

</div>

<?php endwhile; wp_reset_query(); ?>

</div></blockquote>It shows stickys but not just 4, I want the 4 most recent sticky posts to display

Answers (3)

2010-05-11

Oleg Butuzov answers:

<?php
query_posts(
array('post__in'=>get_option('sticky_posts'),
'showposts' => 4 ,
'order' => 'desc',
'orderby' =>'date')
);
?>


Oleg Butuzov comments:

'order' => 'desc' ,
'orderby' =>'date'


is a order fields , you can specify yours...

right now it will take 4 most new posts...


Rick Bible comments:

none of these work, they work no differently than what I already have... see, if my client has 10 stickys checked, I only want the four most recent. My code, and this code, as well as variations below are still showing all the stickys checked.


Oleg Butuzov comments:


add_filter('option_posts_per_page', 'total_posts_in_category');
query_posts(
array('post__in'=>get_option('sticky_posts'),
'showposts' => 4 ,
'order' => 'asc',
'orderby' =>'date')
);
remove_filter('option_posts_per_page', 'total_posts_in_category');
function total_posts_in_category(){
return 4;
}




Oleg Butuzov comments:

setting 4 for everithing!

add_filter('option_posts_per_page', 'totala_posts_in_category');
query_posts(
array(
'post__in'=>get_option('sticky_posts'),
'showposts' => 4 ,
'order' => 'asc',
'orderby' =>'date',
'posts_per_page'=>4
)
);

remove_filter('option_posts_per_page', 'totala_posts_in_category');

function totala_posts_in_category(){
return 4;
}


Rick Bible comments:

No luck, would it be easier to do a tag, say "featured", and show 4 most recent featured tags?


Oleg Butuzov comments:

this is finality. and fatality of your problem

<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 3);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );

if (have_posts()) :
while (have_posts()) : the_post();
// code here
endwhile;
endif;

?>

2010-05-11

Utkarsh Kukreti answers:

Use 'numberposts' => 4

2010-05-11

Erez S answers:

Replace showposts with posts_per_page or numberposts


Erez S comments:

Here is another solution:
$sticky=get_option('sticky_posts') ;
query_posts('p=' . $sticky[0].','.$sticky[1].','.$sticky[2].','.$sticky[3]);
//LOOP

Should work,if not,try to use seperate query_posts for each sticky,and read this for more information:
[[LINK href="http://codex.wordpress.org/Function_Reference/query_posts#Sticky_Post_Parameters"]]http://codex.wordpress.org/Function_Reference/query_posts#Sticky_Post_Parameters[[/LINK]]