0

I have a custom pos type created in functions.php and working great - except I need to have categories enabled. I have looked through other answers here, and am still not able to figure it out. In my code i have taxonomies => array('categories', 'tags') but it is not working.

Here is my code:

function custom_post_type() {

    // Set UI labels for Custom Post Type
    $labels = array(
        'name' => _x('Trade Alerts', 'Post Type General Name', 'Avada'),
        'singular_name' => _x('Trade Alert', 'Post Type Singular Name', 'Avada'),
        'menu_name' => __('Trade Alerts', 'Avada'),
        'parent_item_colon' => __('Parent Trade Alert', 'Avada'),
        'all_items' => __('All Trade Alerts', 'Avada'),
        'view_item' => __('View Trade Alert', 'Avada'),
        'add_new_item' => __('Add New Trade Alert', 'Avada'),
        'add_new' => __('Add New', 'Avada'),
        'edit_item' => __('Edit Trade Alert', 'Avada'),
        'update_item' => __('Update Trade Alert', 'Avada'),
        'search_items' => __('Search Trade Alert', 'Avada'),
        'not_found' => __('Not Found', 'Avada'),
        'not_found_in_trash' => __('Not found in Trash', 'Avada'),
    );

    // Set other options for Custom Post Type

    $args = array(
        'label' => __('trade alerts', 'Avada'),
        'description' => __('Trade alerts with Forex stuff', 'Avada'),
        'labels' => $labels,
        // Features this CPT supports in Post Editor
        'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields'),
        // You can associate this CPT with a taxonomy or custom taxonomy.
        'taxonomies' => array('categories', 'tags'),
        /* A hierarchical CPT is like Pages and can have
         * Parent and child items. A non-hierarchical CPT
         * is like Posts.
         */
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'show_in_admin_bar' => true,
        'menu_position' => 5,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'page',
    );
0

Your code is basically correct, except that the standard taxonomies have singular names.

Use taxonomies' => array('category', 'post_tag') instead.

1
  • Hey, that did the trick - the taxonomies should have been singular, not plural. Thank you so much! Mar 16 '20 at 21:49
0

You should be able to relate category taxonomy to your custom post type with register_taxonomy_for_object_type()

add_action('init','add_categories_to_cpt');
function add_categories_to_cpt(){
    register_taxonomy_for_object_type('category', 'post_type_name');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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