0

I have category 1, category 2, category 3, to category 7 and custom post types Gallery, Article, Front Row..

How can we restrict the admin to choose the category for custom post type Gallery to Cat 1, 2 3 only?

<?php 
    register_post_type('gallery',
        ...
        'taxononmies' => array('category' => array(1, 2, 3))
        ...
    );
?>

1 Answer 1

0

You'd probably be better served by creating a custom taxonomy to accompany your custom post type. That way you can manage terms separate from the built-in categories taxonomy.

// Register Custom Taxonomy
function wpse_168762_create_gallery_taxonomy() {

    $labels = array(
        'name'                       => _x( 'Gallery Categories', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Gallery Category', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Gallery Categories', 'text_domain' ),
        'all_items'                  => __( 'All Gallery Categories', 'text_domain' ),
        'parent_item'                => __( 'Parent Gallery Category', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Gallery Category:', 'text_domain' ),
        'new_item_name'              => __( 'New Gallery Category Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Gallery Category', 'text_domain' ),
        'edit_item'                  => __( 'Edit Gallery Category', 'text_domain' ),
        'update_item'                => __( 'Update Gallery Category', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate Gallery Categories with commas', 'text_domain' ),
        'search_items'               => __( 'Search Gallery Categories', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove Gallery Categories', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used Gallery Categories', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'gallery_category', array( 'gallery' ), $args );

}

// Hook into the 'init' action
add_action( 'init', 'wpse_168762_create_gallery_taxonomy', 0 );

To attach the custom taxonomy on the other end (register_post_type), you would amend your 'taxonomies' parameter to 'taxonomies' => array('gallery_category'). If you needed to attach multiple taxonomies, it would be 'taxonomies' => array('gallery_category','category')

It's still required to register the custom taxonomy for this to work.

2
  • No, when registering a post type attaching a taxonomy is not required (though recommended according to the CODEX, as it might cause unexpected results with parse_query or pre_get_posts). Either way, 'taxononmies' => array('category' => array(1, 2, 3)) isn't correct. Terms are not set here. You would only need 'taxononmies' => array('category'), and manage the terms elsewhere. I've updated my answer to show how you could also attach the custom taxonomy via register_post_type.
    – BODA82
    Commented Nov 18, 2014 at 14:28
  • Thanks.. this concept is also working., But I ended up with the built-in categories.. Commented Nov 18, 2014 at 14:56

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.