Miguel Arias
Miguel Arias

Reputation: 101

Wordpress can't add categories to custom post type

I'm on Wordpress 4.8.1. For some reason when I register the custom post type and I add:

'taxonomies' => ['category'],

It doesn't work. The categories don't show up on the custom post type edit screen. It always worked before so I don't understand why it would not work now. It was even working on the site only a few months ago. Someone help :)

!! UPDATE !!

Here is my code, i realize it is necessary lol

function spanish_cpt() {
  $labels = array(
    'name'               => _x( 'Spanish', 'post type general name' ),
    'singular_name'      => _x( 'Spanish Post', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Spanish Post' ),
    'edit_item'          => __( 'Edit Spanish Post' ),
    'new_item'           => __( 'New Spanish Post' ),
    'all_items'          => __( 'All Spanish Posts' ),
    'view_item'          => __( 'View Spanish Post' ),
    'search_items'       => __( 'Search Spanish Posts' ),
    'not_found'          => __( 'No spanish posts found' ),
    'not_found_in_trash' => __( 'No spanish posts found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'Spanish'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Spanish version of posts',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array('title', 'editor', 'thumbnail',
                             'author', 'comments', 'revisions'),
    'has_archive'   => 'es',
    'taxonomies' => array('category', 'post_tag'),
  );
  register_post_type( 'spanish', $args ); 
}
add_action( 'init', 'spanish_cpt' );

Upvotes: 1

Views: 3048

Answers (2)

Nitesh Gour
Nitesh Gour

Reputation: 136

Use this in functions.php

function register_results() {

    $labels = array(
        'name' => _x('Results', 'post type general name'),
        'singular_name' => _x('Results', 'post type singular name'),
        'add_new' => _x('Add New', 'Results'),
        'add_new_item' => __('Add New Case Results'),
        'edit_item' => __('Edit Case Results'),
        'new_item' => __('New Case Results'),
        'all_items' => __('All Case Results'),
        'view_item' => __('View Case Results'),
        'search_items' => __('Search Case Results'),
        'not_found' => __('No Results found'),
        'not_found_in_trash' => __('No Results found in the Trash'),
        'menu_name' => 'Case Results'
    );
    $args = array(
        'labels' => $labels,
        'description' => 'Results and Results Related information will be hold on this',
        'public' => true,
        'show_in_menu' => true,
        'menu' => 5,
        'menu_icon'=>'dashicons-admin-post',
        'supports' => array('title', 'editor', 'thumbnail', 'post-format', 'excerpt'),
        'has_archive' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'taxonomies' => array('Category_results')
    );
    register_taxonomy(
        'Category_results', 'results', array(
        'hierarchical' => true,
        'label' => 'Custom Category',
        'show_admin_column' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true
    ));
    register_post_type('results', $args);
}

add_action('init', 'register_results');

Upvotes: 1

Miguel Arias
Miguel Arias

Reputation: 101

Well i found out what the problem was. I was using a custom post type helper class in order to generate them in a much cleaner way. the original code was this

$spanish = new CPT([
    'post_type_name' => 'spanish',
    'singular' => 'Spanish Post',
    'plural' => 'Spanish Posts',
    'slug' => 'es'
], [
    'public' => true,
    'menu_icon' => 'dashicons-admin-post',
    'query_var' => true,
    'has_archive' => 'es',
    'hierarchical' => true,
    'can_export' => true,
    'menu_position' => 5,
    'publicly_queryable' => true,
    'rewrite' => [
        'slug' => 'es/%spanish_categories%', 
        'with_front' => false
    ],
    'supports' => [
        'title', 'editor', 'thumbnail',
        'author', 'comments', 'revisions',
    ],
    'taxonomies' => ['category'],
]);

This has always worked but I guess when I updated to 4.8 something broke and it no longer works. So I just switched to the regular way of creating custom post types and it works now. Thanks everyone for your help and participation.

Upvotes: 0

Related Questions