-
Currently there is a method to get the rendered title for page
RankMath\Paper\Paper::get()->get_title()
But this only works within the template modules.
Is there a way to retrieve the rendered title for a specific post ID?
-
Hello,
Thank you for contacting Rank Math support today and bringing your concern to our attention.
You may try to use this code to generate the title using ID:
RankMath\Post::get_meta( 'title', $post->ID );
Let us know how that goes. Looking forward to helping you with this one.
Thank you for your prompt reply! Just tried it and I get an empty string in return. Double check it against a couple of other posts with the same results.
I’ve also tried other keys like ‘canonical_url’ and ‘description’ the same results.
No errors, so that’s good.Hello,
It seems to be working just fine from my end as you can see in my video screencast here: https://i.rankmath.com/vytABn
Can you please share the filter code you have modified?
Also, please consider clearing your website/browser cache and see if it reflects on the front.
Looking forward to helping you on this one.
Getting closer!
So two things, one about your example and then one about the code I’m writing.1. I tried your example out, and added the filter so that the title appends itself to the content when WP is processing the Loop. It works under a specific circumstance.
- If I modify the RankMath title value for the page, even just adding a space to the end of the title, the values show up.
- If I create a new page/post, and just use the default settings for RankMath, the values don’t show up.
Does that happen to you too?
2. The example you gave works as long as the code is in The Loop. I’m writing code for rest end points in WP so my code looks more like this in the functions.php.
add_action('rest_api_init', function () { register_rest_route( 'endpoint/v1', 'action', array( 'methods' => WP_REST_Server::READABLE, 'callback' => 'fetch_action', 'permission_callback' => '__return_true' )); }) function fetch_action($request) { $id = $request->get_param('id'); $page = get_page($id); $response = new stdClass(); $response->title = RankMath\Post::get_meta( 'title', $page->ID ); $response = new WP_REST_Response($response); $response->set_status(200); return $response; }
What ‘RankMath\Post::get_meta’ returns in this case is any custom value that I add to the pages title setting without any of the additional presets.
Example: “%title% %sep% %sitename% mychange” will only return “mychange”, everything else that should have been replaced during rendering is ignored. If I don’t add any values at all, nothing gets returned. Do I have to run it through some filters to get the full rendered title?
Hello,
It seems that the
get_meta
will only return value from individually modified meta, it won’t render value from global/default meta.Allow me to confirm this with our developers. We should get back to you shortly.
We appreciate your time and patience on this one.
Hello,
I just got word from our developers. The
Post::get_meta
will only return the value stored in the postmeta table so if you leave your SEO title empty, it will also return an empty value.Please try using this code instead:
RankMath\Post::get_meta( 'title', $post->ID, RankMath\Paper\Paper::get_from_options( "pt_{$post->post_type}_title", $post, '%title% %sep% %sitename%' ) );
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Very close. Out of the two points that I stated in previous post the first one has been addressed. Which is very excellent. Thank you! I’m posting it below just for other people to reference.
1. To be added to functions.php
function prefix_add_content ($content) { global $post; $someTitle = RankMath\Post::get_meta( 'title', $post->ID, RankMath\Paper\Paper::get_from_options( "pt_{$post->post_type}_title", $post, '%title% %sep% %sitename%' ) ); $content = $content.$someTitle; return $content; } add_filter('the_content', 'prefix_add_content');
2. The above code requires the filter and RankMath functions to be placed within The Loop for it to work properly. Is it possible to have it placed outside The Loop like in my REST API endpoint call I had written above?
Hello,
I am glad that this helped you with your situation.
Could you please let us know which loop are you referring to? Is this the loop for the posts/pages?
The code should work even outside a loop as long as you insert the post ID as your argument. Have you tried doing so?
Looking forward to helping you.
It works fine when I place the code within a template page or component which is excellent. The place that I want to use it in is in the WordPress REST API code.
add_action('rest_api_init', function () { register_rest_route( 'endpoint/v1', 'action', array( 'methods' => WP_REST_Server::READABLE, 'callback' => 'fetch_action', 'permission_callback' => '__return_true' )); }) function fetch_action($request) { $id = $request->get_param('id'); $page = get_page($id); $res = new stdClass(); $res->title = RankMath\Post::get_meta( 'title', $page->ID, RankMath\Paper\Paper::get_from_options( "pt_{$page->post_type}_title", $page, '%title% %sep% %sitename%' ) ); $response = new WP_REST_Response($res); $response->set_status(200); return $response; }
If you make a call to http://localhost/wp-json/endpoint/v1/action/PAGEID (with PAGEID replace with an actual Page ID) you’ll get JSON response with ‘title’ being returned as an empty string.
Hello,
This function is not intended to be used inside a REST API route.
If you would like to use a REST API route you need to enable the Headless CMS option from our plugin and follow the steps illustrated here: https://rankmath.com/kb/headless-cms-support/
Hope this helps clarify your doubts.
Don’t hesitate to get in touch if you have any other questions.
Understood. For my Headless CMS, I was hoping to retrieve a complete set of page data via a single unified API call that included meta data but I guess not. Thank you for answering my questions, and the long path it took to get here.
Hello,
I am glad that we could address your concern.
If you have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
Thank you.
I also have a similar question about this. I used the Headless CMS like you said and it returns all the html in the header tag, which is not optimal at all. The returned result should be json with the variables as a separate data field.
I also have a similar question about this.
I used the Headless CMS like you said and it returns all the html in the header tag, which is not optimal at all. The returned result should be json with the variables as a separate data field.Running on the same issue, here’s my suggest:
function rankmath_meta($str, $postId){
//you can add more variable that you need to render on this…
$str = str_replace(‘%title%’,get_the_title($postId),$str);
$str = str_replace(‘%sitename%’,get_bloginfo($postId),$str);
return $str;
}add_action(‘rest_api_init’, function () {
register_rest_route(‘get’, ‘meta’, array(
‘methods’ => ‘GET’,
‘callback’ => ‘get_meta’,
));
});function get_meta()
{
$postId = $_POST[‘id’]
$post = get_post($postId);
$post->title = rankmath_meta(get_post_meta($postId,’rank_math_title’,true),$postId); //replace all the rank math variable here
return $post;
}Ps: I agree with the guy above, Headless CMS should return JSON format instead of HTML.
You must be logged in to reply to this ticket.