0

I used Justin Tadlock's tutorial on how to create a loop consisting only sticky posts.

The code looked more or less like this:

$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 2 );
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );

According to the tutorial, I simply need to change the last number from the 3rd line to configure how many posts I wanted to display in the loop.

But I think the code above won't let me display number of posts any bigger than what's stored in posts_per_page setting (dashboard -> Settings -> Reading)


So here's the question:

How can I make the query above so that it could display more posts than whatever value the posts_per_page option have?

  • it seems I just found the answer... I simply need to insert posts_per_page into the query_posts() – Leader Jan 17 '15 at 13:24
3

The issue is that you are using the function query_posts this only queries the default result on that page. It's advised that you use wp_query instead, it just eliminates the margin for error or unexpected results.

You can create a new query like below, and specify explicitly how many posts to return:

<?php

$sticky = get_option( 'sticky_posts' );
rsort( $sticky );

$args = array(
    'post__in' => $sticky,
    'posts_per_page' => 10
    );

$sticky_query = new WP_Query( $args );

while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
    // Do stuff with sticky posts
endwhile;

wp_reset_postdata();

This method also allows you to drop the array_slice process. So you can simply change posts_per_page to a number of your choice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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