-
Hi,
This guide page – https://rankmath.com/kb/how-to-automate-the-insertion-of-focus-keyword/#automatically-use-the-post-title-as-the-focus-keyword – has alot of helpful code but it does not have a code to use customfield()
what would the code look like to replace the title like other snippets with %customfield(field-name)%
ex: Tires For Sale: %customfield(tyre_brand)% %customfield(tyre_model)% %customfield(width)%/%customfield(profile)%R%customfield(diameter)% %sep% %sitename%
Thank you
Tino
tiredash.ca
-
Hello,
Thank you for contacting Rank Math support, and sorry for any inconvenience that might have been caused due to that.
You would need to modify the filter a bit to call a custom field name which you can use to automate the insertion of the focus keyword. Please refer to my example code here:
function update_focus_keywords() { $posts = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post' )); foreach ($posts as $p) { $forKw = get_field('my_field_name', $p->ID); // SET YOUR FIELD NAME HERE update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower($forKw)); } } add_action('init', 'update_focus_keywords');Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Hi,
Thank you for responding, I actually created the same modification and same results, crashed the site ? I correct the error its not an issue, the site does not remain crashed. I am using custom plugin to add the snippet
I didnt debug but can. Using version 8php.The other code examples work well on both the custom post types on my site “product” and “acadp_listings” (using the title example code)…but when using this new one for showing the custom meta ..crashes. The custom meta shows well when used for example in settings like: %customfield(width)%/%customfield(profile)%R%customfield(diameter)%
let me know your thoughtsT hx
Hi again,
To simplify what i would need is simple the make model (width/profile/diameter)
this is all i need in my keywords and this comes from user entered meta from the ad listings
these are the customfields
BRAND = %customfield(tyre_brand)%
MODEL = %customfield(tyre_model)%
SIZE = (WIDTH)/(PROFILE)R(DIAMETER) %customfield(width)%/%customfield(profile)%R%customfield(diameter)%This looks like this after fetching info for example
Pirelli Sottozero 205/55R16
Please help me solve this as my experience with writing code cannot get this one.
Thank you
Hello,
We apologize for the inconvenience.
The code I shared is just a placeholder and you would need to further edit that to create the output you need.
Please try this code instead:
function update_focus_keywords() { $posts = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post' )); foreach ($posts as $p) { $forKw = get_field('tyre_brand', $p->ID) . ' ' . get_field('tyre_model', $p->ID) . ' ' get_field('width', $p->ID) . '/' . get_field('profile', $p->ID) . 'R' . get_field('diameter', $p->ID); update_post_meta($p->ID, 'rank_math_focus_keyword', strtolower($forKw)); } } add_action('init', 'update_focus_keywords');Let us know if it works on your end.
Looking forward to helping you.
I just seen that this ticket was answered, i will try this toinight and get back to you guys.
Thank you
Hello,
Sure, please take your time and let us know how that goes.
Thank you.
so, I did try. i just dont understand why site goes down when this code active but not the code for get_the_title….
This works
<?php
if ( ! defined( ‘ABSPATH’ ) ) {
exit;
};/**
* Function to automatically update the focus keyword with the post title
*/
function update_focus_keywords_products()
{
$posts = get_posts(array(
‘posts_per_page’ => -1,
// ‘post_type’ => ‘post’ //replace post with the name of your post type
‘post_type’ => ‘product’ //replace post with the name of your post type));
foreach ($posts as $p) {
update_post_meta($p->ID, ‘rank_math_focus_keyword’, strtolower(get_the_title($p->ID)));
}
}
add_action(‘init’, ‘update_focus_keywords_products’);?>
so does this one…..
<?php
if ( ! defined( ‘ABSPATH’ ) ) {
exit;
};/**
* Function to automatically update the focus keyword with the post title
*/
function update_focus_keywords_listings()
{
$posts = get_posts(array(
‘posts_per_page’ => -1,
// ‘post_type’ => ‘post’ //replace post with the name of your post type
‘post_type’ => ‘acadp_listings’ //replace post with the name of your post type));
foreach ($posts as $p) {
update_post_meta($p->ID, ‘rank_math_focus_keyword’, strtolower(get_the_title($p->ID)));
}
}
add_action(‘init’, ‘update_focus_keywords_listings’);?>
but not this one?…ps. there was an error in the most recent snippet you had sent me, I corrected it
error was here
‘ ‘ get_field(‘width’, $p->ID)
corrected to
‘ ‘ . get_field(‘width’, $p->ID)<?php
if ( ! defined( ‘ABSPATH’ ) ) {
exit;
};function update_focus_keywords_products_custom() {
$posts = get_posts(array(
‘posts_per_page’ => -1,
‘post_type’ => ‘product’
));
foreach ($posts as $p) {
$fields = get_field(‘tyre_brand’, $p->ID) . ‘ ‘ . get_field(‘tyre_model’, $p->ID) . ‘ ‘ . get_field(‘width’, $p->ID) . ‘/’ . get_field(‘profile’, $p->ID) . ‘R’ . get_field(‘diameter’, $p->ID);
update_post_meta($p->ID, ‘rank_math_focus_keyword’, strtolower($fields));
}
}
add_action(‘init’, ‘update_focus_keywords_products_custom’);?>
we are overlooking something simple. it must be the type of post
The custom meta we are calling in the above code is created by a plugin i use which is dependent on metabox pluigin.
could this have to do with anything?thankyou
TinoHello,
Could you confirm if even after you added the missing concatenation (.) operator, the issue still persists and the website is going down?
we are overlooking something simple. it must be the type of post
The code shared by my colleague is meant to run on posts only.
The custom meta we are calling in the above code is created by a plugin i use which is dependent on metabox pluigin.
could this have to do with anything?Could you create a screencast about this so that we can better understand the situation? You can use a tool like loom to create the screencast.
Looking forward to hearing back from you.
Thank you.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.
The ticket ‘How to Automate the Insertion of Focus Keyword – Custom meta option’ is closed to new replies.