0
votes

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.

2

2 Answers

0
votes

If you register a taxonomy, you can choose the post type, to which the taxonomy should be registered. https://codex.wordpress.org/Function_Reference/register_taxonomy

In your case this would be pages. This will affect all pages, because they are all of the type "page".

You can create another Post Type if you want to have that seperated from the pages.

With your function, you can add another type to be registered with the taxonomy. This register_taxonomy_for_object_type is to add an already registered taxonomy to an object type. We are taking about types, so it is correct behaviour that the taxonomy is now available for all pages.

I'm not quite sure if I got your question right... but if you want to assign a page template to a single specific page, you can just select the page template in the page attributes when editing a page in backend.

So you can either create another post type for pages where you only want to have a taxonomy, or you can make page templates to edit the template for single pages. Don't really know how this is meant in your question.

Maybe you can write more details what exactly you are trying to do and what problems you ran in.

0
votes

If I understand correctly from your question and your comments, what you're looking for is an archive for that custom taxonomy:

Rather than starting from a blank file, it is good practice to copy the next file in the hierarchy, if it exists. If you’ve already created an archive.php, make a copy called category.php and modify that to suit your design needs. If you don’t have an archive.php file, use a copy of your theme’s index.php as a starting point.

Taken from: https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/

So you'd basically take the archive.php, create a copy and name it consultants_category.php - then within that file layout and display the content how you need it to be displayed. Use the archive.php file as a starting point and modify as needed. When you do it this way it is pre-built to take your consultants_category taxonomy, identify which specific term in that taxonomy it needs to display, and then output the layout you've specified.