Search code examples
phpwordpresscustom-wordpress-pagescustom-taxonomy

Need to assign custom taxonomy to the pages with specific php template - Wordpress


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.


Solution

  • Developer by saeed Hanif

    To assign a custom taxonomy to pages in WordPress and display those pages using a specific template, you can follow these steps:

    Create a custom taxonomy:

    In your theme's functions.php file, use the register_taxonomy() function to create a custom taxonomy. For example:

    function create_page_taxonomy() {
      $labels = array(
        'name'              => _x( 'Page Types', 'taxonomy general name' ),
        'singular_name'     => _x( 'Page Type', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Page Types' ),
        'all_items'         => __( 'All Page Types' ),
        'parent_item'       => __( 'Parent Page Type' ),
        'parent_item_colon' => __( 'Parent Page Type:' ),
        'edit_item'         => __( 'Edit Page Type' ),
        'update_item'       => __( 'Update Page Type' ),
        'add_new_item'      => __( 'Add New Page Type' ),
        'new_item_name'     => __( 'New Page Type Name' ),
        'menu_name'         => __( 'Page Types' ),
      );
    
      $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'page-type' ),
      );
    
      register_taxonomy( 'page_type', array( 'page' ), $args );
    }
    add_action( 'init', 'create_page_taxonomy', 0 );
    

    Add the code to display the pages with the custom taxonomy in the body of the template file:

    $page_type = get_query_var( 'page_type' );
    $pages = get_pages( array(
      'tax_query' => array(
        array(
          'taxonomy' => 'page_type',
          'field' => 'slug',
          'terms' => $page_type,
        ),
      ),
    ) );
    
    foreach ( $pages as $page ) {
      // Output page title and content
      echo '<h2>' . $page->post_title . '</h2>';
      echo apply_filters( 'the_content', $page->post_content );
    }