1

Here's my code:

<?php query_posts(array ('showposts' => '1', 'cat' => '-11'));
  if (have_posts()) : while (have_posts()) : the_post();
      get_template_part( 'content', 'archives' );
     endwhile;
  endif; ?>

<?php query_posts(array ('showposts' => '3', 'offset' => '1'));
  if (have_posts()) : while (have_posts()) : the_post();
      get_template_part( 'content', 'archives' );
     endwhile;
  endif; ?>

The first query excludes a category (user contributions to the blog) so that only our organisation's latest blog post is 'stickied'.

The second query currently displays the latest 3 posts from all categories with an offset of 1 to stop the 'stickied' post being displayed, as it's already the latest.

However, if the most recent post is then in the User Contributions category (we sometimes publish user content later in the day), it will be caught by the 'offset' and not displayed, and the stickied post will then appear at the front of that second query.

Is there an alternative to 'offset' that allows the exclusion of a category from the rule?

TL;DR - I want the second query to exclude whatever the result of the first query is

  • 3
    As a side note - you shouldn't use query_posts. Use WP_Query or get_posts instead. – Dan Bough Aug 26 '14 at 15:49
  • Thanks Dan, I made use of WP_Query and that lead me to create an additional array to use in the second query. See the answer below :) – tristanojbacon Aug 26 '14 at 16:17
1

As already stated, you should never (my emphasis) use query_posts to construct custom queries.

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose.

Just two side notes on your question and answer (which is way better use of code than your question BTW :-))

  • showposts has been depreciated in favor of posts_per_page

  • NEVER forget to reset any custom query. As described in your answer, the output is expected from your code as you have not reset your queries. Custom queries influence any query after it if the postdata is not reset. Use wp_reset_postdata() after every custom query

This is how your code should look like

<?php $sticky = new WP_Query(array ('posts_per_page' => '1', 'cat' => '-11'));
if ($sticky->have_posts()) : 
    $stickyid = array(); 
while ($sticky->have_posts()) : $sticky->the_post(); 
$stickyid[] = $post->ID;
get_template_part( 'content', 'archives' );
endwhile; endif;
wp_reset_postdata();

$latestposts = new WP_Query(array ('posts_per_page' => '3', 'post__not_in' => $stickyid));
if ($latestposts->have_posts()) : 
    while ($latestposts->have_posts()) : $latestposts->the_post();
get_template_part( 'content', 'archives' );
endwhile; endif; 
wp_reset_postdata(); ?>
  • Hi Pieter, I ended up using your solution, and it fixed the problem. Thank you! – tristanojbacon Dec 30 '14 at 13:32
1

EDIT: Did not solve the problem, see note at bottom So after much trial and error, I managed to solve the problem. Here's the final code (an adaptation of the one in the question):

<?php $sticky = new WP_Query(array ('showposts' => '1', 'cat' => '-11'));
if ($sticky->have_posts()) : 
    $stickyid = array(); 
while ($sticky->have_posts()) : $sticky->the_post(); 
$stickyid[] = $post->ID;
get_template_part( 'content', 'archives' );
endwhile; endif;

$latestposts = new WP_Query(array ('showposts' => '3', 'post__not_in' => $stickyid));
if ($latestposts->have_posts()) : 
    while ($latestposts->have_posts()) : $latestposts->the_post();
get_template_part( 'content', 'archives' );
endwhile; endif; ?>

It may not be the tidiest of code, but it makes sense to me! :S

EDIT: Turns out this isn't entirely correct. For some reason, the excerpts for each post on the page are using the excerpt from the most recent post (irregardless of whether or not it's stickied). Any suggestions as to how that can be corrected would be great!

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.