Support

Account

Home Forums Backend Issues (wp-admin) ACF with custom Titles & Yoast SEO

Solving

ACF with custom Titles & Yoast SEO

  • Greetings. Thank you for the awesome product! I <3 ACF so much!

    Here’s what I’m trying to do. I have a Zoo website I’m working on and the animals have a custom post type that I’m using. The animals use the Title for their name but I also have the scientific name as an ACF field. What I want to do is have the Title of the page be the Animal name – Scientific name – Site Name. I see Yoast SEO uses %%title%% and %%sitename%% and that it can call in custom fields with %%cf_<field name>%% but I just didn’t see a way for me to be able to call in ACF fields similar to the %%cf_<field name>%%. Is this possible or do I need to just hard code it into the header.php file? I was hoping to do it the former to both automate it and allow the client to see it on the back end editor.

    Thank you again,
    Jeremy

  • You should be able to replace `<field_name>’ with the name you’ve given the field that holds the scientific name.

    for example, if you’ve name the field scientific_name then enter %%cf_scientific_name%% into the title template field for the post type.

  • Following on from this, I have a Post Object as my custom field that I want to retrieve the title from. Using cf_field_name will only return the ID of that Post Object – what do I have to do to lookup that post and add the title from that post as a custom field which is available to Yoast?

    Any ideas?

    Thanks,

  • @lucpestille for that I think you would need to use the SEO hook for modifying the post title, but you’ll need to do a bit more checking

    
    add_filter('wpseo_title', 'change_seo_title');
    function change_seo_title($string) {
      $queried_object = get_queried_object();
      if (!isset($queried_object->ID || 
          get_post_type($queried_object->ID) != 'your-post-type-here')) {
        // bail early
        return $string;
      }
      $related_post = get_field('post_object_field_name', $queried_object->ID);
      if ($related_post) {
        $title = get_the_title($related_post->ID);
        $string .= ' - '.$title;
      }
      return $title;
    }
    
  • Excellent – worked almost straight out of the box – thanks a million. For anyone else who needs it, my final code was;

    
    function change_seo_title($string) {
      $queried_object = get_queried_object();
      if ( !isset($queried_object->ID) || get_post_type($queried_object->ID) != 'post' ) {
        // bail early
        return $string;
      }
      $related_post = get_field('product', $queried_object->ID);
      if ($related_post) {
        $related_post_title = get_the_title($related_post->ID);
        $new_title = $related_post_title . " - " . $string;
      }
      return $new_title;
    }
    add_filter('wpseo_title', 'change_seo_title');
    
  • How will these and other YoastSEO related approaches work out when Yoast releases their new version of the plugin, somewhere in November. They are going to remove a lot of filters in favour of faster page analysis, one of these is the ‘wpseo_pre_analysis_post_content’ filter and that one is used by a lot of ACF developers.
    Can someone from the ACF team comment on this?

    This is what Yoast is going to do:
    https://yoast.com/yoast-seo-breaking-api-changes/

  • I’m trying to use this but custom post type still showing ID instead of title.

    Any suggestion or help ????

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘ACF with custom Titles & Yoast SEO’ is closed to new replies.