I'm looking for a solution similar to here, except I want to keep attachment pages for attachments in a specific category.
I tried adding the below function, but it's still redirecting all of them:
add_action( 'template_redirect', 'test_attachment_redirect', 10 );
function test_attachment_redirect() {
if( is_attachment() && !is_category('keep')) {
$url = wp_get_attachment_url( get_queried_object_id() );
wp_redirect( $url, 301 );
}
return;
}
For context, my attachments do have categories with this function:
function add_categories_to_attachments() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_to_attachments' );
is_category
will never work for this. Also keeping in mind that an attachment can appear in a post in that category, but not be attached, since attachments can be used in multiple places, but can only have 1 parent post. What's the problem that doing this solves for you? Perhaps there are alternatives that can be suggested you are unaware of