I am attempting to get the result of wp_title for a specific url. I am doing this to form an ajax responce when a page is requested.

I have read several responces (e.g. Get wp_title() from page ID into a variable) which is great except I am using Yoast SEO which (I believe) overides the default function.

I would like, somehow, to get the title that YOAST would spit out.

If I am inside the loop, calling wp_title gives me the desired result. Like I said, however, I am doing this outside the loop. I have tried the following:

function requestPage() {
    $url = esc_html($_GET['pageChange']);
    if($url == "/"):
        $url = get_home_url();
    $postID = url_to_postid( $url );
    global $post;
    $post = get_post($postID);
    setup_postdata( $post );
add_action("wp_ajax_page_change", "requestPage");
add_action("wp_ajax_nopriv_page_change", "requestPage");

Thanks in advance for any help


I have also tried doing the request in a custom loop but it is still not working. This is how I tried it:

$query = new WP_Query(array(
    'p'         => $postID,
    'post_type' => 'any'
    while ($query->have_posts() ): $query->the_post();
        echo "I get here";

I get the responce I get here so I am sure the query is working but I am still not getting the title.

Edit 2:

After looking at the inner workings of wp_title, I noticed that it is actually using the global wp_query variable rather than the post variable. I suppose this makes sence as the title needs to work on archive / search pages as well as post pages.

For test purposes, I checked the query for the about-us page on the site and reproduced it. After doing var_dumps and diffs, I have confirmed that the query is now the same if I do it in the head section of the about us page or in the ajax function. This is how I do it:

$query = new WP_Query(array(
    'page'              => '',
    'pagename'          => 'about-us'
global $wp_query;
$wp_query = $query;
var_dump($wp_query);  //This is the same as a var_dump on about us page

I now get the responce » About us. This is almost right but it is still not getting the changes that Yoast makes. Using the Yoast plugin, I have set the title to Test.

I have checked and the Yoast plugin uses the wp_title hook which is how they make their changes. Is there any reason that the plugins hooked functions wouldn't be run in an ajax environment? If so, is there any way around this?

The Yoast plugin changes are working on the main page. In my header.php I have the following:

<title><?php wp_title();?></title>

The HTML source has this:


1 Answer 1


YOAST's SEO Title is stored within a meta_key. Try this-

$taxonomy = get_queried_object()->taxonomy;
$term_id = get_queried_object()->term_id;
$meta   = get_option( 'wpseo_taxonomy_meta' );
$title  = $meta[$taxonomy][$term_id]['wpseo_title'];

Or this-

$titles = get_option( 'wpseo_titles' );

Your Answer

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

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