1

I would like to show sticky posts at the top of an archive list for a specific category on the top page as well as on archive pages. I've been Googling for a working solution and found a few clues similar to this:

if(!empty(get_option('sticky_posts'))){
    $args = array(
        'posts_per_page' => -1,
        'posts__in'      => get_option('sticky_posts'),
        'category_name'  => 'EVENTS'
    );

    $catquery = new WP_Query($args);
} 

The above code only works if 'category_name' => 'EVENTS' is commented out, so it seems as if you cannot show sticky posts at the top of the archive list as long as a category is called. Although some wrote something like 'WordPress can only show sticky posts on the front page.', but this does not seem to be the case making me terribly confused.

Someone please help me work this out.

Thank you in advance.

0

As per documentation for WP_Query(), category_name should be category slug. So replace EVENTS with the slug of category EVENTS

'category_name'  => 'events'  // Assuming that 'events' is slug of category `EVENTS`

OR

'cat'  => 5'   // Replace 5 with id of  of category `EVENTS`

I hope this helps.

  • Yes, the slug for the EVENTS category is indeed 'events', but setting 'category_name' => 'events' does not work either. I also tried setting 'cat' => 3 given that the category ID of 'events' is 3 with no luck. All the time I had one post associated with the same category set to sticky and confirmed setting other posts to sticky make no difference — they all show up in the reverse chronological order. – Fizzler Apr 2 at 7:18
  • There must be some other issue, the code is seems to be correct. This may help. Check if the slug is event or events. – Qaisar Feroz Apr 2 at 7:34
  • Thank you. The page you game the link for makes perfect sense. Still my code doesn't work for sticky posts and I checked the category settings once again only to find no error: ibb.co/GJSMDvX In the end I may resort to giving up. It's been helpful to be double-checked by someone else. – Fizzler Apr 2 at 8:43

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.