0

I'm working on a blog with hundreds of old posts, and new posts being added daily. I can't change their flow of filling out the yoast meta tag for title. They want the og:title value to be something else though, a custom post_meta value from the same post.

How can I hook into yoast before they grab the title, and say "Don't use the yoast title, instead if we have a custom title filled out, use this one."

If this isn't possible, an alternative option would be to just output a duplicate og:title tag either before or after yoast, so that the yoast tag is ignored. Would I place my tag before or after to wipe out the yoast tag, and would there be a negative impact from having the two tags?

1 Answer 1

0

Use the wpseo_opengraph_title filter:

function wpse_187763_wpseo_opengraph_title( $title ) {
    if ( is_singular() && $post = get_queried_object() ) {
        if ( $_title = get_post_meta( $post->ID, 'custom_field_key', true ) )
            $title = $_title; // Override title with custom meta title
    }

    return $title;
}

add_filter( 'wpseo_opengraph_title', 'wpse_187763_wpseo_opengraph_title' );

Update: The single = is intended, since we're assigning the value & testing the expression at the same time. In other words, it's the same as:

$_title = get_post_meta( $post->ID, 'custom_field_key', true );
if ( $_title )
    $title = $_title;
2
  • I'm going to test your code now. Shouldn't the if statements have double == signs, not single? May 8, 2015 at 18:55
  • Nah it's intended (see revision). May 8, 2015 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.