1

I am building a Wordpress site and have come to a bit of a wall while using a WP_Query, it started out quite simple but then sticky posts got thrown into the mix and it's becoming a bit harder to control.

I'm using the default posts, with categorisation and looping through the posts (currently) like this -

  <?php
  // Query for sticky posts
  $sticky = get_option( 'sticky_posts' );
  $the_query = new WP_Query( 'p=' . $sticky[0]); ?>
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php // Sticky posts content
    the_title(); ?>

  <?php endwhile; wp_reset_postdata(); ?>

  <?php
  // Query for remaining posts
  wp_reset_query();
  $categories = get_sub_field('category');
  $args = array(
    'post_type' => 'post',
    'posts_per_page' => '2',
    'ignore_sticky_posts' => 1,
    'post__not_in' => array($sticky[0]),
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'cat_id',
            'terms'    => $categories,
        ),
    ),
  );
  $query = new WP_Query( $args );
  if ( $query->have_posts() ) : ?>
  <?php while ( $query->have_posts() ) : $query->the_post(); ?>

    <?php // Non-sticky posts content
    the_title(); ?>

  <?php endwhile; ?>
  <?php endif;wp_reset_postdata(); ?>

At the moment this work's okay for posting the sticky posts and then following up with the remaining posts, but I need to get it working with a little more functionality. Firstly, on the home page I would only like 2 posts to be displayed, but currently if I set the posts per page to 2, it shows the sticky posts plus 2 of the non-stick posts, I'm trying to make it display, for example if there was 1 sticky post, the sticky posts and one non sticky post, or if there are no sticky posts, just 2 non sticky posts (I hope that makes sense!). So I guess that's my first question, I'm not entirely sure it's possible?

My second question is regarding pagination. On another page from where I am displaying the 2 posts above, I'm looking to display 6 posts per page, then have navigation to display the next 6 and so on. This works for the first page, but for the following pages it continues to show the sticky post at the top of every page, rather than just the first, I'm not sure if this is possible either, especially as this page is also having the issue of showing 6 posts per page plus sticky posts, as opposed to 6 posts per page including sticky posts. (I'm going to add the code I have for the 6 per page + pagination incase it helps!)

  <?php
  // Query for sticky posts
  $sticky = get_option( 'sticky_posts' );
  $the_query = new WP_Query( 'p=' . $sticky[0]); ?>

  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php // Sticky posts content
    the_title(); ?>

  <?php endwhile; wp_reset_postdata(); ?>

  <?php
  // Query for remaining posts
  wp_reset_query();
  $categories = get_field('categories');
  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  $args = array(
    'post_type' => 'post',
    'posts_per_page' => '6',
    'ignore_sticky_posts' => 1,
    'post__not_in' => array($sticky[0]),
    'paged'=> $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'cat_id',
            'terms'    => $categories,
        ),
    ),
  );
  $query = new WP_Query( $args );
  if ( $query->have_posts() ) : ?>

    <?php // Non-sticky posts content
    the_title(); ?>

    <?php endwhile; wp_reset_postdata(); ?>

    <?php next_posts_link( 'Older Entries', $query->max_num_pages ); ?>
    <?php previous_posts_link( 'Newer Entries' ); ?>

  <?php endif; ?>

Thanks for any help, and sorry if you run out of breath trying to read this, I tried to have as much detail as possible and have a tendency to overuse commas in place of full stops.

Thanks again, Harry.

  • 1
    +1 for a great formatted question :-). I have answered a very similiar question which should address both your issues. I believe this is your homepage, so get rid of your custom queries, and revert to pre_get_posts . Check my answer here – Pieter Goosen Jun 1 '15 at 16:37
  • Thanks very much, I will have a read of your answer now. The first part of my question is regarding the homepage whereas the second part relates to a normal page. I will let you know how I get on and update my question with my findings, thanks again! – Harry Francis Jun 2 '15 at 8:16

Your Answer

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

Browse other questions tagged or ask your own question.