0

I'm looking for a way to bring a post if it's in a certain category to the front (or top) of the list. The category is called 'first'. I don't think orderby supports category so what other options do I have? Should I create another query inside the first query?

Thanks,

5
  • 1
    Sticky posts? You can also rerun the loop Commented Jul 10, 2016 at 9:45
  • Don't want to do sticky posts as I want as little interaction with the admin menu as possible. Can you provide an example of rerunning the loop? Commented Jul 10, 2016 at 10:06
  • 1
    Have a look at this post. You can use in_category() to test for your specific category. With regards to sticky posts, you can set a post as sticky without admin interface. Commented Jul 10, 2016 at 10:18
  • 1
    Gregory, if you want all the posts from first category displayed first and followed by all the posts not in that category, knif3r's answer is what you are looking for, but that functionality is already provided out of the box by sticky posts. You don't need a category. If, however, you want a normal flow of WP posts and if on that particular page there is a post from first category to be displayed first, it's doable, but a bit more complicated, as you will have to apply a filter on the results of your query before running the loop.
    – tao
    Commented Jul 10, 2016 at 11:47
  • This question has continued - wordpress.stackexchange.com/questions/232029/… Commented Jul 25, 2016 at 10:02

1 Answer 1

0

You will have to use 2 queries, where in the first loop you should use:

$args = array(  
  "post_type" => "post",
  "post_status" => "publish",
  "orderby" => "date",
  "order" => "DESC",
  "posts_per_page" => 20,
  'post__in' => cat,
);
(The Query)

And in the second:

$args = array(  
  "post_type" => "post",
  "post_status" => "publish",
  "orderby" => "date",
  "order" => "DESC",
  "posts_per_page" => 20,
  'post__not_in' => cat,
);
(The Query)

This should work because the first loop will take only the posts from cat and the second loop all others, and the first loop will always stay on top, Altought I don't know if you won't have a problem with the paginations that way, you should also ensure that you reset the post data with wp_reset_query(); for example.

2
  • There is no reason to use two queries Commented Jul 10, 2016 at 10:49
  • He wants all posts from cat to be displayed before other posts, for me the best way is to use 2 queries or sticky posts.
    – knif3r
    Commented Jul 10, 2016 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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