1

we have a redirect function for a Custom Post Type to redirect single view pages for certain terms back to their archive landing page. There should only be single view pages for one specific taxonomy, all others only show snippets on the archive landing pages. But WP still creates the single pages for all created posts, and sometimes search engines find them, so we need to stop that.

Here's what we currently have, which DOES work for ONE of the taxonomy terms that should redirect. The problem now is that there are some posts that have NO terms assigned, and those need to redirect as well. Not sure how to target this.

If it's easier to target ALL instances EXCEPT the one term that does not redirect, that term is 'Team' (which just happens to be the same name as the post type).

if( is_singular( 'team' ) && has_term('support', 'team-area') ) {
wp_redirect( home_url('/who-we-are/'), 301 );
exit();
}
}
add_action( 'template_redirect', 'team_redirect' );

1 Answer 1

0

It's a little unobvious, but term in has_term() is optional.

You can express if post has any term from taxonomy as has_term( null, 'team-area' ).

This is due to it using is_object_in_term() internally, which allows such broad check.

4
  • Hmm, well I don't really want to set a redirect for posts that have ANY term applied. I want to set the redirect for posts that have either one particular term ('support'), OR have NO terms selected. There is one term ('team') that should definitely NOT trigger the redirect.
    – Kenny J
    Commented Sep 1, 2016 at 1:23
  • And I have shown you how to check for any terms, by reversing that you check for no terms. Add that to your logic condition accordingly?
    – Rarst
    Commented Sep 1, 2016 at 7:49
  • I apologize, but I'm not really fluent in PHP, so not exactly how to properly 'reverse' that and/or add it to my existing function. I had 'help' creating that initial code. Hope that's OK here...
    – Kenny J
    Commented Sep 1, 2016 at 21:50
  • That's pushing it a bit since you really need some PHP proficience to deal with this stuff. If I follow your question right it would be something like is_singular( 'team' ) && ( has_term('support', 'team-area') || ! has_term( null, 'team-area' ) ) (if it's a team and has either support area or no area at all).
    – Rarst
    Commented Sep 1, 2016 at 22:00

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.