0

I learnt to add multiple custom post types via functions.php in an array, like this:

add_action('init', 'all_custom_post_types');

function all_custom_post_types() {
  $types = array(
        array('the_type' => 'event',
            'single' => 'Event',
            'plural' => 'Events'
        ),
        array('the_type' => 'resource',
            'single' => 'Resource',
            'plural' => 'Resources'
        ),
        array('the_type' => 'team',
            'single' => 'Team',
            'plural' => 'Team'
        ),
  );

All well and good, and the options for these post types can be declared as follows:

  foreach ($types as $type) {
    $the_type = $type['the_type'];
    $single = $type['single'];
    $plural = $type['plural'];
    $labels = array(
      'name' => _x($plural, 'post type general name'),
      'singular_name' => _x($single, 'post type singular name'),
      'add_new' => _x('Add New', $single),
      'add_new_item' => __('Add New '. $single),
      'edit_item' => __('Edit '.$single),
      'new_item' => __('New '.$single),
      'view_item' => __('View '.$single),
      'search_items' => __('Search '.$plural),
      'not_found' =>  __('No '.$plural.' found'),
      'not_found_in_trash' => __('No '.$plural.' found in Trash'),
      'parent_item_colon' => ''
    );
    $args = array(
      'labels' => $labels,
      'public' => true,
      'publicly_queryable' => true,
      'show_ui' => true,
      'query_var' => true,
      'rewrite'  => array( 
         'with_front' => false
      ),
      'capability_type' => 'post',
      'hierarchical' => true,
      'has_archive' => true,
      'menu_position' => 5,
      'supports' => array('title','editor','author'),
      'show_in_menu' => true,
      'show_in_nav_menus' => true,
      'taxonomies' => array( 'category', 'post_tag' ),
    );
    register_post_type($the_type, $args);
  }
}

But what if I want the taxonomies => array( 'category', 'post_tag' ), line to apply to the resource post type only? In this instance I'd like to enable taxonomies as follows:

  • resource: categories AND tags
  • event: tags ONLY
  • team: no taxonomy terms

Can this be done the way I've declared my custom post types, or should I be declaring them separately to achieve more granular control?

1 Answer 1

2

I have tweaked theyour code and it should work.. please give it a try

add_action('init', 'all_custom_post_types');

function all_custom_post_types() {
  $types = array(
        array('the_type' => 'event',
            'single' => 'Event',
            'plural' => 'Events'
        ),
        array('the_type' => 'resource',
            'single' => 'Resource',
            'plural' => 'Resources'
        ),
        array('the_type' => 'team',
            'single' => 'Team',
            'plural' => 'Team'
        ),
  );
foreach ($types as $type) {
    $the_type = $type['the_type'];
    $single = $type['single'];
    $plural = $type['plural'];
    $labels = array(
      'name' => _x($plural, 'post type general name'),
      'singular_name' => _x($single, 'post type singular name'),
      'add_new' => _x('Add New', $single),
      'add_new_item' => __('Add New '. $single),
      'edit_item' => __('Edit '.$single),
      'new_item' => __('New '.$single),
      'view_item' => __('View '.$single),
      'search_items' => __('Search '.$plural),
      'not_found' =>  __('No '.$plural.' found'),
      'not_found_in_trash' => __('No '.$plural.' found in Trash'),
      'parent_item_colon' => ''
    );
    $args = array(
      'labels' => $labels,
      'public' => true,
      'publicly_queryable' => true,
      'show_ui' => true,
      'query_var' => true,
      'rewrite'  => array( 
         'with_front' => false
      ),
      'capability_type' => 'post',
      'hierarchical' => true,
      'has_archive' => true,
      'menu_position' => 5,
      'supports' => array('title','editor','author'),
      'show_in_menu' => true,
      'show_in_nav_menus' => true,
    );

    if($the_type=='resource'){
        $args['taxonomies'] = array( 'category', 'post_tag' );
    }elseif($the_type=="event"){
        $args['taxonomies'] = array( 'post_tag' );
    }else{
    }
    register_post_type($the_type, $args);
  }
}
7
  • ah yes – this removes cats and tags from all CPTs but resource, thanks. as per my question i was looking to allow tags only on the event CPT as well. i figured that appending an elseif ($the_type=="event"){$args['taxonomies'] = array( 'post_tag' );} would have made that happen, but no?
    – mtm
    Commented Feb 2, 2022 at 12:03
  • hey @mtm , glad you figured it out. Though I modified my answer too for your exact case. If this helped you , it will be great i you can accept the answer or vote up atleast. Commented Feb 2, 2022 at 13:50
  • thanks – thing is, that's not working. i've just updated with your tweak too but the same problem persists. no tags on event post type. any ideas?
    – mtm
    Commented Feb 2, 2022 at 14:46
  • I tweaked the code again and above code is working perfect for me Commented Feb 2, 2022 at 15:03
  • yes that's working now but for some reason the 'tags' menu item isn't present under 'events' in the admin menu, although both taxonomy items are present for resources. do you have any idea why/how to fix?
    – mtm
    Commented Feb 2, 2022 at 17:18

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.