0

I reused the following code where a specific taxonomy is registered:

public static function get_taxonomies_to_register() {
    $taxonomies = apply_filters( 'wp_taxonomies', array(

        'wp_course' => array(
            'name'               => _x( 'Courses', 'taxonomy general name', 'test' ),
            'singular_name'      => _x( 'Course', 'taxonomy singular name', 'test' ),
        ),

        'wp_kind' => array(
            'name'               => _x( 'kinds', 'taxonomy general name', 'test' ),
            'singular_name'      => _x( 'kind', 'taxonomy singular name', 'test' ),
        ),

    ));

    return $taxonomies;
}

I want to dynamically register all taxonomies of my custom post type, so I used the following code:

function get_new_taxonomies ( $taxonomies) {

    return get_object_taxonomies('my_custom_post_type'); ;
}

add_filter('wp_taxonomies', get_new_taxonomies );

$wp_taxonomies_result = WP_Taxonomies::get_taxonomies();

But i got only two taxonomies: Course and Kind. What am I doing wrong and how can I fix it?

2
  • I don't understand. get_object_taxonomies() returns taxonomies that have been registered. Why would you want to register them again? Commented Mar 10, 2019 at 2:35
  • i just try to use plugin who create custom post type with specific taxonomy and i try to add taxonomies of another custom post type to the custom post type created by the plugin , i don't know if i'm clear now. Commented Mar 10, 2019 at 7:45

1 Answer 1

0

You forgot to add the existing taxonomies to the array you're returning. try this:

function get_new_taxonomies ( $taxonomies) {

        return array_merge($taxonomies, get_object_taxonomies('my_custom_post_type'));
    }
add_filter('wp_taxonomies', get_new_taxonomies );

$wp_taxonomies_result = WP_Taxonomies::get_taxonomies();
1
  • i try it but it doesn't work , i guess that it's due to the fact that get_object_taxonomies returns array of taxonomy and in other hand $taxonomies variable in get_taxonomies_to_register is array of array so i guess i need function who return array of array of my taxonomies. Commented Mar 9, 2019 at 18:54

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.