The following code displays 8 posts on the 1st page. On all succeeding pages however, only 7 posts are displayed. I am assuming this is because the 1st page also displays sticky posts (whereelse the succeeding pages do not).

  • How can that problem be solved (I need 8 posts an all pages)?
  • Alternatively, the sticky posts should be displayed on all pages, not only on the first?

My original code:

$header_query = new WP_Query(
    'orderby=date&posts_per_page=7&paged='.$page_to_load.'&ignore_sticky_posts=0
');

EDIT: Implementation of Daniel Sachs' suggestion:

// query db
$header_query1 = new WP_Query(
    array('post__in' => get_option('sticky_posts'), 'posts_per_page' => 1));
$header_query2 = new WP_Query(
    array( 'post__not_in' => get_option( 'sticky_posts' ), 'posts_per_page' => 7, 'orderby' => date, 'paged' => $page_to_load));

//display
getPostsFromQuery($header_query1);
getPostsFromQuery($header_query2);

function getPostsFromQuery($header_query) {
    if ( $header_query->have_posts() ) : 
    // loop etc.
}

I am still looking for a solution that requires only one database access? And for the alternative solution with sticky posts only on page 1.

link|improve this question

17% accept rate
feedback

1 Answer

I'd suggest using two queries, one for sticky posts, another for all the others.

First:

query_posts(array('post__in' => get_option('sticky_posts'), posts_per_page => 1));

then query all the other posts

query_posts( array( 'post__not_in' => get_option( 'sticky_posts' ), posts_per_page => 7, orderby => date, paged => '.$page_to_load.'));

this should do it.

link|improve this answer
I have implemented it just to try it (see original post). One needs to use WP_Query instead of query posts, and your quotes are not used correctly (with your code, pagination won't work). I would still be interested in a one-access solution? – Ben Jun 18 '11 at 8:57
I also still need the alternative solution for displaying sticky posts only on page 1, but having an equal amount on posts on all pages. I cannot do posts_per_page=7 on the 1st page, and then posts_per_page=8 on the next, because this breaks pagination. So what alternative is available? – Ben Jun 18 '11 at 9:09
I don't think you ever find a single loop solution. I don't think there is one. – Daniel Sachs Jun 18 '11 at 22:05
BTW you don't need to use WP_Query, you also can $header_query1 = query_posts(array( ... just remember to rewind_posts(); – Daniel Sachs Jun 18 '11 at 22:11
Aha, ok, thank you. Have you by any chance also got an idea for the alternative version with sticky posts only on page 1? – Ben Jun 19 '11 at 9:51
feedback

Your Answer

 
or
required, but never shown

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