user2093301
user2093301

Reputation: 367

Restrict posting in one taxonomy

I have two taxonomies for a custom post type. One is categories and the other is called types. I would like to restrict the author to be able to select one of two taxonomies.

For example, if the author selects one of the "Types" taxonomy, he would not be able to select "Categories"

Is this possible?

Upvotes: 1

Views: 392

Answers (1)

Ravinder Kumar
Ravinder Kumar

Reputation: 944

You need to create custom taxonomy for custom post type only: Here is working code for me. It will display only custom taxonomy for custom post type.

// Registers the new post type and taxonomy
function wpt_packages_posttype() {
  register_post_type( 'Packages',
    array(
      'labels' => array(
        'name' => __( 'Packages' ),
        'singular_name' => __( 'Packages' ),
        'add_new' => __( 'Add New Packages' ),
        'add_new_item' => __( 'Add New Packages' ),
        'edit_item' => __( 'Edit Packages' ),
        'new_item' => __( 'Add New Packages' ),
        'view_item' => __( 'View Packages' ),
        'search_items' => __( 'Search Packages' ),
        'not_found' => __( 'No Packages found' ),
        'not_found_in_trash' => __( 'No Packages found in trash' )
      ),
      'taxonomies' => array('package-Category'),
      'public' => true,
      'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
      'capability_type' => 'post',
      'rewrite' => array("slug" => "packages"), // Permalinks format
      'menu_position' => 5,
    )
  );
}

add_action( 'init', 'wpt_packages_posttype' );





function add_console_taxonomies() {

  register_taxonomy('package-Category', 'review', array(
    // Hierarchical taxonomy (like categories)
    'hierarchical' => true,
    // This array of options controls the labels displayed in the WordPress Admin UI
    'labels' => array(
      'name' => _x( 'Package Category', 'taxonomy general name' ),
      'singular_name' => _x( 'Package Category', 'taxonomy singular name' ),
      'search_items' =>  __( 'Search Package Categories' ),
      'all_items' => __( 'All Package Categories' ),
      'parent_item' => __( 'Parent Package Category' ),
      'parent_item_colon' => __( 'Parent package-Category:' ),
      'edit_item' => __( 'Edit Package Category' ),
      'update_item' => __( 'Update Package Category' ),
      'add_new_item' => __( 'Add New Package Category' ),
      'new_item_name' => __( 'New Package Category Name' ),
      'menu_name' => __( 'Package Categories' ),
    ),

    // Control the slugs used for this taxonomy
    'rewrite' => array(
      'slug' => 'package-Category', // This controls the base slug that will display before each term
      'with_front' => false, // Don't display the category base before "/locations/"
      'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
    ),
  ));
}
add_action( 'init', 'add_console_taxonomies', 0 );

Upvotes: 1

Related Questions