How to add a theme custom variable to a post title?

I assume that you want to use propertysq custom field/post meta in the title and you use Yoast SEO. ( Correct me if I’m wrong )

So, use this is your functions.php

function wpse239252_hook_title($title) {
global $post; // make sure the post object is available to us
if(is_singular()){ // check we're on a single post
  $propertysq = get_post_meta(get_the_ID(), "propertysq", true);
  if($propertysq != "") { //check if not empty
    $title = $propertysq.' - '.$title;
  }
}
return $title;
}

add_filter('wpseo_title', 'wpse239252_hook_title', 15, 1);

This will add propertysq field to your title as 1,002Sq Mt - HS0525 - Chaweng Noi - Horizon Homes Koh Samui in your given example, where propertysq = 1,002Sq Mt.

P.S: To get post_meta propertysq, use $propertysq = get_post_meta(get_the_ID(), "propertysq", true);

Let me if it works for you.

Leave a Comment