0

When we click a category link we find all posts under that category. I would like to hide posts from specific tag within that category, and to show a link which enable us to show the posts on that tag (which was hidden) within category. With jQuery and tabs I can achieve this, but there is a problem with pagination. So I need to find another way.

Example: We have 10 posts; 5 with Category A and 5 with Category B.

Of those, 3 posts have Tag C (2 from Category A and 1 from Category B).

When I click category A will show 3 posts (hidding the two tagged 'C') and afterwards (when I'm in category page) if I click Tag C it will show 2 posts (not 3, because 1 post has category B).

Is this possible?

0

You could modify your query on pre_get_posts.

Example:

// I'd wouldn't hardcode the tags/category ids, but where to store and 
// how to retrieve those settings go beyond the scope of this question.

define( 'YIVIS_EXCLUDED_TAG', 12 );

add_action( 'pre_get_posts', 'yivi_excludes_tags' ); 

function yivi_excludes_tags( $query ) {

  if ( $query->is_category() {
     $query->set('tag', [ YIVIS_EXCLUDED_TAG *  -1 ] );
  }

}

With this, every time you are performing a category based query posts tagged with that tag would be excluded from the search. To exclude certain categories on a tag view should be easy enough going on from here.

  • i posted the answer but it was deleted. (dont know why). I was looking for filter and a plugin made this possible. – user24903 Jan 16 '16 at 14:27
  • The above code allow you to accomplish this without using an additional plugin. If your answer consisted on "install this or that plugin" it's not very helpful for fellow coders. – yivi Jan 17 '16 at 7:25

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.