I created in Wordpress custom taxonomy called 'consultants_category' and assigned it to custom post type. Now I need to assign this taxonomy to pages with specific php template - 'page-consultants.php'.
Wordpress have function to assign custom taxonomy called 'register_taxonomy_for_object_type' (https://developer.wordpress.org/reference/functions/register_taxonomy_for_object_type/) but when I use it - my taxonomy (custom categories) are shown on the backend of all wp pages and I don't know how to take pages only with specific page template.
Here is rough idea of what I need to achive (get all wp pages -> filter all pages to get pages only with specific php template -> assign custom taxonomy only to filtered pages ):
function add_taxonomies_to_pages() {
$pages = get_pages(
array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-consultants.php'
)
);
foreach($pages as $page){
register_taxonomy_for_object_type( 'consultants_category', 'pages');
}
}
add_action( 'init', 'add_taxonomies_to_pages' );
Would appreciate any help. Thanks.