I’m trying to add a custom field to a post title (sample page). To do so, I’m following the instructions given in this previous question, but the variable is not appearing in the title as expected. In fact, the exact same title appears. Any help would be appreciated.

The previous instructions indicate that the following code should be added to functions.php:

function wpse_224340_document_title($title){
    global $post; // make sure the post object is available to us
    if(is_singular()){ // check we're on a single post
      $release_author = get_post_meta(get_the_ID(), "release_author", true);
      if($release_author != "")
        { $title["title"].= " (" . $release_author. ")";}
    }
    return $title;
}
add_filter("after_setup_theme", function(){ add_theme_support("title-tag"); });

The variable in my case is propertysq, not release_author. But a simple find/replace resulted in the following…

What occurs when I implement this code?

The current page title is: HS0525 - Chaweng Noi - Horizon Homes Koh Samui This title has been inserted automatically by a WordPress plugin “Yoast SEO.” But after I disable this automatic title insertion, and insert the above code, the title inserted onto the page is identical to the previous page title: HS0525 - Chaweng Noi - Horizon Homes Koh Samui

Possible Sources of Error

Possible Source of Error #1: The aforementioned plugin/mechanism that is forcing its own <title> onto the page. I am currently researching if this is the cause. I’d like to avoid completely removing the plugin, but I may have to.

Possible Source of Error #2: Would I need to edit the following line to correspond to my theme/functions?

$release_author = get_post_meta(get_the_ID(), "release_author", true);

I tried replacing it with the following line, but it did not work.

$propertysq = ale_get_meta('propertysq');

I should note:

  • This site does not have a proper test environment setup where I can easily see PHP errors and dump vars.
  • Per the previous answer, I also remembered to comment out the <title> tag in my header.php.

edit: Here is the exact code I’ve inserted into functions.php:

add_filter("document_title_parts", "wpse_224340_document_title");
function wpse_224340_document_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 != "")
        { $title["title"].= " (" . $propertysq. ")";}
    }
    return $title;
}
add_filter("after_setup_theme", function(){ add_theme_support("title-tag"); });

2 Answers
2

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 Reply

Your email address will not be published. Required fields are marked *