0

The Setup


I have divided my blog into 3 categories with their respective slugs. There are 3 pages corresponding to these 3 categories with their respective custom page templates. On each page, I want to loop through just the posts that correspond to that category.

So let's say I have a page for the "videos" category:

$videoPosts = new WP_Query( array(
       'posts_per_page' => get_option( 'posts_per_page' ),
       'category_name' => 'videos'));

Then I stuck that into the loop just below:

if ( $videoPosts->have_posts() ) : 
  while ( $videoPosts->have_posts() ) : $videoPosts->the_post(); ?> 
    <h2><?php the_title(); ?></h2>
  <?php endwhile;
endif;

wp_reset_postdata();

This does output all the posts by order of date, however, as Pieter Goosen has shown, is_home is set to false because I'm querying a specific category.

The Problem? Sticky Posts!

In the videos category for instance, I have 2 sticky posts, and, as previously described, they won't stick because I'm querying a specific category.

I tried creating two loops:

query1 sticky posts in video category
query2 non-sticky posts in video categry

Loop 1 query1->posts
Loop 2 query2->posts

This set up works, except it messes up pagination becase (from my understanding) the two loops are conflicting. When I put:

next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );

...nothing happens.

As you may be able to tell, I'm a complete beginner with wordpress. With my limited knowledge, I can't get Pieter Goosen's solution to work. Wyck suggested using WP_list_pluck but I can't understand how to work it.

So...

Is there some way to merge the two queries so that Sticky Posts come up first followed by non-sticky posts by date with most recent, while also limiting number of posts per page all in one query and loop (therefore allowing pagination)?

Could one use merge array?

At the very least, if there is no simpler alternative, would you please be willing to expand on how wp_list_pluck would be implemented in this situation for idiots like myself?

Thank you so much!

2
  • Are you wanting to do something different with the sticky posts or do you just want them to be the first ones in the loop?
    – drdogbot7
    Apr 2 '21 at 15:26
  • No alternative formatting or anything, just for them to be at the front of the loop, to actually stick.
    – Z. Steam
    Apr 2 '21 at 16:07

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.