I have related post section in my theme. I would like to exclude posts from a certain tag.

This is my query:

'post__in'            => $related_posts_ids,
'orderby'             => 'post__in',
'posts_per_page'      => $max_posts_to_show,
'ignore_sticky_posts' => true,

Using tag__not_in is working but the number of posts doesn't stay same. I mean, total number of posts is 6. After excluding 1 post from that tag, total number of posts becomes 5.

If I remove the post__in, number of posts stays same but then related post section doesn't work as expected.

Is there a way to keep post__in, exclude posts from a certain tag and keep the number of posts same?

  • Are there enough posts that fit the criteria or is the change in number simply saying that there are only a total of 5 posts? – kero Mar 23 '18 at 14:46
  • Yes. If not enough posts, query completes it with the new posts. That's why I want to use this query. Removing post__in is working but as I said, then query doesn't do that job. – ahmet Mar 23 '18 at 14:49
  • 1
    Logically I don't see how this is going to work, or that what you're trying to do is sensible/makes sense, you specified which posts it needs to fetch, so it fetched them, why not exclude those posts via PHP? Keep in mind that anything with __not_in is going to be extremely slow and expensive. It would be faster to define a list of every other tag and look for those posts – Tom J Nowell Mar 23 '18 at 15:01
  • Thank you for explanation. Actually this related post section is very useful. It displays related posts by tag and if not enough posts then completes it with the new posts. However I use another section below this. I display featured posts. So I want to exclude posts if have the tag featured. Unfortunately posts with the tag featured show up in both sections. – ahmet Mar 23 '18 at 15:06

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.