filters - use apply_filters return taxonomies custom post type

admin2025-06-02  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?

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?

Share Improve this question edited Mar 10, 2019 at 1:43 samdpedraza 235 bronze badges asked Mar 9, 2019 at 16:58 alpha.romeoalpha.romeo 491 gold badge1 silver badge5 bronze badges 2
  • I don't understand. get_object_taxonomies() returns taxonomies that have been registered. Why would you want to register them again? – Jacob Peattie 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. – alpha.romeo Commented Mar 10, 2019 at 7:45
Add a comment  | 

1 Answer 1

Reset to default 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();
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748824824a314031.html

最新回复(0)