How To

Yoast Custom Meta with CPTs

Making for better SEO without having to think, just by using some elbow grease.

Remember Monday when I was learning about how I was ignorant of SEO from OnPage?

Right so here were the odd keyword notes it told me I needed to take care of and I had no idea what was going on. I mean, I looked at this and stared:

  • Add the keyword kima greggs in the meta title
  • Add the keyword kima greggs in the meta description
  • Add the keyword kima greggs to the content of your page
  • Add the keyword kima greggs in the headlines (<h1> to <h6>)
  • Add images and include the keyword kima greggs in the image’s ALT tag

Then I did what every intelligent person does when faced with an unknown to-do. I read directions.

The title tag defines the title and is displayed as the page name on the browser tab. The title is very important for search results. It is the heading used to display the search result and is crucial for the ranking.

Now I actually knew, from having gone through every single tab in Yoast SEO, that titles and metas are handled from the plugin (Yoast > Titles & Meta > Post Types) and, by default, they’re set to be this: %%title%% %%page%% %%sep%% %%sitename%%

Which translates to: “Pagetitle Number — Sitename”

Or in the case of Kima, it’s “Kima Greggs — LezWatchTV” (since I never have any numbered pages for those).

Cool, right? So it was there. Done. I also knew that the name was in the headlines. It wasn’t (and isn’t) in the content of the page, but I’ll accept that SEO hit since contextually it doesn’t work for what I’m writing. Similarly the image thing I handled by having the one image uploaded to the custom post type be the character photo with an alt tag and title of the character name.

You see how I’m cheating.

That left me with the meta description and a new question. You see, what I wanted the description to be would be “Kima Greggs, character on the following TV shows: [list of shows]” and what I didn’t want to do was manually type that for 1000 characters. Who would, right? Again, I knew Yoast had a way to customize that!

Titles and Meta Descriptions for Yoast SEO

You can see in the above screenshot I already changed the title to remove %%page%% since I know the pages will never be paginated. But the “Meta description template” I needed to address. First is the easy part. I want to document that the character is on a TV show. Fine: %%title%% is a character on a TV show and for TV shows, I could do %%title%% is a TV show and that worked.

Of course, I wanted to add what stations a TV show was on, which meant I needed to use %%ct_<custom-tax-name>%% which lists the post’s custom taxonomies, comma separated. Except it didn’t seem to pick up things for my custom post types. Turns out this is a bug.

When I added this: %%title%% is a TV Show on %%ct_lez_tags%% %%sep%% %%excerpt_only%%

It displayed this: <meta name="description" content="Adventure Time is a TV Show on Cartoon Network &ndash; Adventure Time, c&#039;mon grab your friends. We&#039;ll go to very distant lands."/>

That was the easy stuff. When I got to characters, it became a lot messier because of how data was stored. To do the basics I looked at what my custom fields were and came up with this:

%%title%% is a %%ct_lez_sexuality%% %%cf_lezchars_type%% character played by %%cf_lezchars_actor%% on %%cf_lezchars_show%% %%sep%% %%excerpt%%

Now that should have turned into “Jane is a Lesbian guest character played by Anonymous on Fake Show…” but what actually happened was “Jane is a Lesbian guest character played by Array on Array…”

I knew why. Those two values are arrays. Which meant I had to come up with some new code.

function lez_retrieve_actors_replacement( ) {
	if ( !is_array (get_post_meta( get_the_ID(), 'lezchars_actor', true)) ) {
		$actors = array( get_post_meta( get_the_ID(), 'lezchars_actor', true) );
	} else {
		$actors = get_post_meta( get_the_ID(), 'lezchars_actor', true);
	return implode(", ", $actors);

function lez_register_yoast_extra_replacements() {
	wpseo_register_var_replacement( '%%actors%%', 'lez_retrieve_actors_replacement', 'basic', 'A list of actors who played the character, separated by commas.' );

add_action( 'wpseo_register_extra_replacements', 'lez_register_yoast_extra_replacements' );

What this does is creates a new template variable called %%actors%% and that lists the actors with their names separated by commas (yes, some people have multiple actors, have you even heard of US soaps?). The one for shows is more complicated since it’s a post type referencing another post type (shows and characters are post types), but it’s the same general concept.

In the end I went with this: %%title%% is a %%ct_lez_gender%% %%ct_lez_sexuality%% %%cf_lezchars_type%% character played by %%actors%% on %%shows%% %%sep%% Clich├ęs: %%ct_lez_cliches%%

It crams a lot of information into a small place, but it’s also all the important stuff.

One reply on “Yoast Custom Meta with CPTs”

Thanks, wouldn’t have read this but since Yoast shared, (as usual that’s a good measure of worth) I took the time, thanks for sharing. I have a WordPress powered blog for my podcast shownotes, that uses Yoast so this is a good bit of information for my efforts going forward.
Keep on keeping on.

Comments are closed.