Custom meta Title for custom post type archive from page

I've set up some custom post types, and created custom archives, eg in the following format:

archive-books.php (With a template name of "Books Template"). I then have a page (/books/) created in wordpress, with the above archive-books.php assigned as the template.

How would I get the the meta title (wp_title) to be the title that I set on the page (/books/)? - At the moment it's just showing "books | sitename". Even if I change the All in One SEO custom title.

I am using All in One SEO plugin also, but that doesn't seem to work. What I THINK is happening, is that it's just ignoring the page and jumping directly to the archive-books.php as it matches the Wordpress custom post type naming rule?

Topic custom-post-type-archives title plugin-all-in-one-seo custom-post-types Wordpress

Category Web


For anyone using All in one SEO plugin and registered the CPT's via PHP, here's what you can do.

In the CPT registration, inside the labels array, add;

'archive_seo_title' => 'your set title | your site',
'archive_meta_desc' => 'your meta description'

and then in your functions.php file add these 2 filters;

// CPT Archive page SEO Title
add_filter( 'aioseop_title', 'dd_rewrite_custom_titles' );
function dd_rewrite_custom_titles( $title ) {
  if ( is_post_type_archive() ) {
    $post_type = get_post_type_object( get_post_type() );
    if ($post_type->labels->archive_seo_title){
      $title = $post_type->labels->archive_seo_title;
    }
  }
  return $title;
}

// CPT Archive page meta Description
add_filter( 'aioseop_description', 'filter_aioseop_description'); 
function filter_aioseop_description( $description ) { 
    if ( is_post_type_archive() ) {
      $post_type = get_post_type_object( get_post_type() );
      if ($post_type->labels->archive_meta_desc){
        $description = $post_type->labels->archive_meta_desc;
      }
  }
  return $description; 
}

These filters will first check if the current page is a CPT archive and if it determines that it is, it will set the seo title and description to what you set in the labels array during CPT registration. If the filter does not detect a CPT archive page, then it will just return the original titles + description that is set by default.

Note: If you are not using CPT php registration, you can just make $title and $description variables equal to whatever you want, e.g.

$title = get_the_title() . ' | ' . get_bloginfo();

Well I felt I better update this old question, the SEO plugin by Yoast now allows assigning of all SEO abilities to custom post type archives. So that's the best solution.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.