Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to retain tag taxonomy for a single custom post type, but am struggling with it.

Is there a way to remove this meta box for everything BUT?


remove_meta_box('tagsdiv-post_tag', 'post', 'normal');

Will remove the tags from normal posts (desired), but it means they're also removed from ALL custom post types. I can remove the meta box for individual custom post types, like so:

remove_meta_box('tagsdiv-post_tag', 'cars', 'normal');

remove_meta_box('tagsdiv-post_tag', 'hotels', 'normal');

etc.

But that means I have to leave tags available on normal posts (undesired).

So I guess I want something like this, to remove the meta box from everything apart from (e.g.) airports:

remove_meta_box('tagsdiv-post_tag', '!airports', 'normal');

Any ideas?

share|improve this question
    
Do you just want to remove the meta box but not the tags taxonomy of these CPTs? Also: Have you tried removing it everywhere and then just adding it back for this one CPT? –  kraftner Jun 6 at 14:58
    
Have you tried using Advanced Custom Fields? It is THE most incredible plugin you will ever use for Custom Fields and once you have created your Custom Post Type, you have the option of switching off certain meta boxes. –  SixfootJames Jun 6 at 14:59
    
I'm using ACF, but it doesn't help with tags, unless all you need to do is select from a pre-defined list of tags - I looked into this first :) @kraftner - this would be fine - is there a true 'add_meta_box' for this context? Docs seemed to indicate it's usage was different –  Nathan Hornby Jun 6 at 15:08

3 Answers 3

up vote 0 down vote accepted

Why not just turn off the default tags capability then register a tags taxonomy specific to the custom post type?

http://codex.wordpress.org/Function_Reference/register_taxonomy

register_taxonomy(
  'my_custom_post_type_tags',
  'my_custom_post_type',
  array(
    'label'        => __( 'Tags' ),
    'rewrite'      => false,
    'hierarchical' => false,
    'capabilities' => array( 'edit_terms' => 'manage_categories' )
  )
);
share|improve this answer
    
This is technically an improvement - not quite what I asked for - but it is the best way to achieve what I need so I'm going to mark this is correct. For anyone that needs to do exactly what I requested then @The Sufi's answer does the job (with my correction). Thanks Joey, works like a charm! –  Nathan Hornby Jun 10 at 15:02

You need to check either the current post is airports or not.

This can be done like this:

function custom_remove_tag_box() {
    global $current_screen;
    if ( $current_screen->post_type != 'airports' ) {
        remove_meta_box('tagsdiv-post_tag', 'post', 'normal');
    }
}
add_action('admin_head', 'custom_remove_tag_box');

Tested and working.

share|improve this answer
    
Ah, simple as that, thanks! I'm just leaving the office for the weekend (bad timing) but will be picking this up again Monday, so will report back then ;) –  Nathan Hornby Jun 6 at 15:29
3  
This cannot work. admin_init does not pass a WP_Post object. –  toscho Jun 6 at 15:43
    
@toscho Thanks for pointing out. I have updated my code, and looks like it's working for me (tested). –  тнє Sufi Jun 6 at 19:54
    
Replacing add_action('admin_head', 'custom_remove_tag_box'); with add_action('admin_menu', 'custom_remove_tag_box'); got it working. –  Nathan Hornby Jun 10 at 14:56

You can achieve removing metaboxes from all post type screens except specific ones. This is how I done it. Might be a bit overrated, lol

Firstly, you will need to get a list of all public post type registered. This is done by using get_post_types. You will need to use the parameter 'public' => true, otherwise all post types will be returned, which is unnecessary.

Secondly, you need to remove or exclude the post type you don't want the metabox to be removed from.

Thirdly, you need to pass the returned list of post type names to the remove_meta_box() function to remove the specific meta box from these post type screens. This function can be used to remove any meta boxes conditionally

<?php
if (is_admin()) :
function my_remove_meta_boxes() {
    if( current_user_can('manage_options') ) {
    $custom_post_types = get_post_types( array( 'public' => true ), 'objects');

        foreach ( $custom_post_types as $custom_post_type ) {

            $exclude = array( 'information' ); //<---Replace information with your own post type

            if( TRUE === in_array( $custom_post_type->name, $exclude ) )
            continue;

            $posttypes = $custom_post_type->name;

            remove_meta_box('tagsdiv-post_tag', $posttypes, 'normal');
        }
    }
}
endif;

add_action( 'admin_menu', 'my_remove_meta_boxes' );
share|improve this answer
    
Definitely a tad over complex :) –  Nathan Hornby Jun 10 at 15:04
    
Hahaha, thought so. Great that one of the other answers helped :-) –  Pieter Goosen Jun 10 at 15:05
    
Thanks for your help anyway! –  Nathan Hornby Jun 10 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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