$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>filters - use apply_filters return taxonomies custom post type|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

filters - use apply_filters return taxonomies custom post type

matteradmin11PV0评论

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();
Post a comment

comment list (0)

  1. No comments so far