DanG
DanG

Reputation: 197

Adding Taxonomy details to a custom post type - to get SEO details to show

  1. I created a simple catalogue of memorial posts using (Wordpress Custom Post Type)
  2. Settings has archive set to - TRUE
  3. Created a single-memorial-wall.php
  4. Created an archive-memorial-wall.php

Everything works, but Yoast is unable to help my client with SEO for this archive page, as it's not got a page or category to attach itself to and as such (view source code image) the page title is incorrect, and no description details show up for the page.

So I thought, I could sort this creating a custom taxonomy...(code below) and then make this default for these post types.

However, by just creating a taxonomy, I just add more un necessary complexity, the client has to of course, create a category type, and attach each post to it like with regular posts. (even though all the custom posts will only ever go to this taxonomy).

The reason for the CPT was to give the client a simple dashboard without the need to tick categories, where the simple posts he creates ALWAYS attach to the correct category archive and this be SEO indexable.

What am I missing here... It would be great if the taxonomy area i've created didn't allow for further categorisation and was just the place where the SEO for the has_archive lived.

enter image description here

function custom_memorial_post_type()
{
    $args = array(
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'exclude_from_search' => true,
        'show_in_nav_menus' => false,
        'has_archive' => true,
        'hierarchical' => true,
        'labels' => array(
            'name' => 'Memorials',
            'singular_name' => 'Memorial',
            'add_new' => __('Add A Memorial'),
            'add_new_item' => __('New Memorial'),
            'edit_item' => __('Edit The Memorial'),
            'new_item' => __('New Memorial'),
            'all_items' => __('All Memorials'),
            'view_items' => __('View Memorials'),
            'menu_name' => 'Memorials'
        ),
        'supports' => array(
            'title',
            'custom-fields',
            'post-formats'
        ),
        'description' => 'Pet memorial',
        'menu_position' => 22,
        'menu_icon' => 'dashicons-format-image',
    );
    register_post_type('memorial-wall', $args);
}
add_action('init', 'custom_memorial_post_type');

function memorial_wall_taxonomy() {
    $args = array(
        'labels' => array(
            'name' => 'Memorial Wall',
            'singular_name' => 'Memorial Wall',
            'parent_item' => __('Parent Memorial Wall Category'),
            'parent_item_colon' => __('Parent Memorial Wall Category:'),
            'edit_item' => __('Edit The Memorial Wall Category'),
            'update_item' => __('Update The Memorial Wall Category'),
            'add_new_item' => __('Add New Memorial Wall Category'),
            'new_item_name' => __('New Memorial Wall Category'),
            'menu_name' => 'Memorials Wall SEO'
        ),
        'public' => true,
        'hierarchical' => true
    );
    register_taxonomy('memorial-taxonomy', array('memorial-wall'), $args);
}
add_action( 'init', 'memorial_wall_taxonomy');

Upvotes: 1

Views: 214

Answers (1)

amarinediary
amarinediary

Reputation: 5459

It would be great if the taxonomy area i've created didn't allow for further categorisation and was just the place where the SEO for the has_archive lived.

You can specify a set of capabilities using capabilities argument and since Wordpress 5.5, you can set a default term using the default_term argument. Creating therefore a readonly/invisible taxonomy.

$capabilities = array(
  'manage_terms' => '', //default: manage_categories
  'edit_terms' => '', //default: manage_categories
  'delete_terms' => '', //default: manage_categories
  'assign_terms' => '', //default: edit_posts
);
$default = array(
  'name' => '_your_custom_term_name_',
  'slug' => '_your_custom_term_slug_',
  'description' => '_your_custom_term_description_',
);
$args = array(
  //...other arguments
  'capabilities' => $capabilities,
  'default_term' => $default,
);

Everything isn't pink and pretty tho, as default_term is relatively new, some bugs have been reported. You can read more about it here on the Wordpress ticket reporting platform for example ticket n°51666. I've also wrote a post about it: Wordpress, default_term parameter for register_taxonomy, checkbox not visually checked

Upvotes: 0

Related Questions