0

Trying to modify the output of the WP_Title through a custom post type with a filter. I've had no issues doing it to the_title but I assume as the wp_title function is called through the header it's the problem and I'm missing something simple.

What I want to do is change the output of the listing pages with some of the meta-box data into the pages title tag. What I've set up so far is as follows:

add_filter('wp_title', 'dw_listing_title', 10, 2);
function dw_listing_title($title, $id) {

if('sp_property' == get_post_type($id) )
    $address = get_post_meta($id, 'Address', false );
    $city = get_post_meta($id, 'City', false );
    $listingID = get_post_meta($id,"ListingId");

    $title = $address[0] . ', ' . $city[0] . ' Listing: ' . $listingID[0];

return $title;
}

This alters the title just fine when I switch wp_title to the_title. I think I need to add a global variable to tie into it but when I tried adding global $post (or $wp_query) nothing changed.

Also Im not sure if Yoast SEO would cause problems with trying to change this? I dont have any data entered into the input boxes though.

EDIT:: So after turning off the Yoast SEO plugin the wp_title is being rewritten, the problem now is the meta box values being displayed.

1 Answer 1

1

The filter wp_title won't pass $id to your function. Drop the parameter and instead retrieve it with get_queried_object_id():

function dw_listing_title( $title ) {
    if ( is_singular() && $id = get_queried_object_id() ) {
       // Modify $title as required
    }

    return $title;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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