3

I set up a custom post type and taxonomy for Person which worked fine. I added quite a few posts until I decided I wanted to change the name to People. The posts I had added disappeared so I had to reassign the posts to the correct post type.

The problem I have now is the taxonomies are not displaying (location and job-function ). They don't appear to be registered and don't show in the menu or on the custom type post page.

I have tried resetting permalinks and used flush_rewrite_rules(); but still nothing. Can anyone help?

<?php   
/**
 * People post type & taxonomies
 *
 * Post Type:   people
 * Taxonomies:  function
 *            
 */
       
add_action( 'init', 'create_people_post_type', 5 );
function create_people_post_type() {            
    flush_rewrite_rules();
    register_post_type( 'People',
        [
            'labels' => [
                'name' => __( 'People' ),
                'singular_name' => __( 'People' ),
            ],
            'public' => true,
            'has_archive' => true,
            'supports' => ['title', 'thumbnail', 'author' ],
        ]
    );
    
    register_taxonomy( 'job-functions',
        'People',
        [
            'labels' => [
                'name' => __( 'Job Functions' ),
                'singular_name' => __( 'Job Function' ),
            ],
            'hierarchical' => true,
            'show_admin_column' => true,
        ]
    );
}
    
add_action('init','add_locations_to_people');
function add_locations_to_people(){
    register_taxonomy_for_object_type('location', 'People');
}

/**
 * Location post type 
 *
 * Post Type:   location
 *             
 */
add_action( 'init', 'create_office_location_post_type', 4 );
function create_office_location_post_type() {
    register_post_type( 'location',
        [
            'labels' => [
                'name' => __( 'Office Locations' ),
                'singular_name' => __( 'Office Location' ),
            ],
            'public' => true,
            'has_archive' => true,
            'supports' => [ 'title', 'author' ],
        ]
    );
}
Saurin Dashadia
  • 1,140
  • 11
  • 25
LeeTee
  • 6,401
  • 16
  • 79
  • 139

1 Answers1

1

When you register new post type, you need to set which taxonomies it would have.

register_post_type( 'People',
        [
            'labels' => [
                'name' => __( 'People' ),
                'singular_name' => __( 'People' ),
            ],
            'public' => true,
            'taxonomies'=>array('job-functions'),
            'has_archive' => true,
            'supports' => ['title', 'thumbnail', 'author' ],
        ]
    );

UPD:

Here is final code for your case. In your first given code you register post type first, then you try to add taxonomy to already registered post type. So, you just need to replace their places.

add_action( 'init', 'create_people_post_type', 5 );
function create_people_post_type() {
    flush_rewrite_rules();
    register_taxonomy(
        'job-functions',
        'People',
        [
            'labels' => [
                'name' => __( 'Job Functions' ),
                'singular_name' => __( 'Job Function' ),
            ],
            'hierarchical' => true,
            'show_admin_column' => true,
        ]
    );
    register_post_type( 'People',
        [
            'labels' => [
                'name' => __( 'People' ),
                'singular_name' => __( 'People' ),
            ],
            'public' => true,
             'taxonomies'=>array('job-functions'),
            'has_archive' => true,
            'supports' => ['title', 'thumbnail', 'author' ],
        ]
    );
}
add_action('init','add_locations_to_people');
function add_locations_to_people(){
    register_taxonomy_for_object_type('location', 'People');
}

UPD2:

add_action( 'init', 'create_people_post_type', 5 );
function create_people_post_type() {
    flush_rewrite_rules();
    register_taxonomy(
        'job-functions',
        'People',
        [
            'labels' => [
                'name' => __( 'Job Functions' ),
                'singular_name' => __( 'Job Function' ),
            ],
            'hierarchical' => true,
            'show_admin_column' => true,
        ]
    );

    register_taxonomy(
        'location',
        'People',
        [
            'labels' => [
                'name' => __( 'Location' ),
                'singular_name' => __( 'Location' ),
            ],
            'hierarchical' => true, //true if it is category like, false if it is  tag like
            'show_admin_column' => true,
        ]
    );
    register_post_type( 'People',
        [
            'labels' => [
                'name' => __( 'People' ),
                'singular_name' => __( 'People' ),
            ],
            'public' => true,
             'taxonomies'=>array('job-functions','location'),
            'has_archive' => true,
            'supports' => ['title', 'thumbnail', 'author' ],
        ]
    );
}
Elvin Haci
  • 3,503
  • 1
  • 17
  • 22
  • Hi, Seems we are half way there as the 'job-functions' is now showing, but 'locations' isn't. I have tried adding 'location' to the taxonomies array but that doesn't work. – LeeTee Oct 31 '17 at 14:27
  • i thought that it is trivial and you will add it yourself. Ok check my UPD2. You don't need additional add_action for location. It can be done all-in-one function. – Elvin Haci Oct 31 '17 at 14:39
  • The locations taxonomy is based on an existing 'locations' custom post type. What you have done is just create a new taxonomy called locations, but this wont use the locations fI have already created as a CPT. Hope that makes sense. The original code I had above worked prior to me changing the CPT name, so I still don't understand how it stopped working? – LeeTee Oct 31 '17 at 15:00
  • Which part of the code of your website does create Location taxonomy for another custom post type ? It is important to know. Because of priority queue. If you don't need to register location l as it already exists, we should know how and where it becomes existing. Your add_locations_to_people function runs with 10 priority, and create_people_post_type runs with 5. We need to know what priority location registerer function has – Elvin Haci Oct 31 '17 at 15:11
  • Ok, Ive tried amending the priorities but still no luck. Code added above – LeeTee Oct 31 '17 at 15:46
  • it seems you added incomplete code to the update of question. please complete it and show which function created location taxonomy – Elvin Haci Oct 31 '17 at 16:58
  • There is some misunderstanding here, @LeeTee. "Location" in your given code is CPT. And you want to use CPT as a taxonomy? :) It is against of WP logic and is impossible without custom-integrator. Such as https://wordpress.org/plugins/cpt-onomies/ May be you used to use such kind of plugin before? – Elvin Haci Nov 01 '17 at 15:04
  • This is very strange, I definitely havent had any plugin for this and I am pretty sure this worked previously. Maybe I'm going mad. Thanks for all your help anyway :) – LeeTee Nov 01 '17 at 15:20