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?
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();
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