简体   繁体   中英

Wordpress - Custom Post Type mess up - taxonomy not showing

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');
}

}

<?php
/**
 * 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' ],
            ]
        );
    }

    ?>

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' ],
        ]
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM