Bulk Focus Keyword Issue with thousands of posts

#464816
Viewing 15 replies - 1 through 15 (of 16 total)
  • Hello,

    Thanks for contacting us and sorry for any inconvenience that might have been caused due to that.

    If you have a very high number of posts, the function can cause such issues.

    To fix this, you can add an offset and define the number of posts to be processed at once and this should fix the issue.

    Here’s a code that you can use to set the post tag as the focus keyword with an offset value:

    /**
     * Function to automatically update the focus keyword with the post tags
     */
    function update_focus_keywords()
    {
        $posts = get_posts(array(
            'posts_per_page' => 1000,
            'post_type' => 'post' //replace post with the name of your post type
            'offset' => 0 // Increase this by 1000 each time.
        ));
         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');

    Please make sure to remove the previously used code to add the focus keyword. Also, make sure to increase the offset value by 1000 each time you run the code.

    The above function adds the focus keyword to 1000 posts at once (‘posts_per_page’ => 1000).

    Please let us know how that goes.

    Hello Thanks for the front replay, I added this code because I want to use Post tittle as Focus keyword.

    <?php
    /**
    * Function to automatically update the focus keyword with the post title, if no focus keyword is set
    */
    function update_focus_keywords()
    {
    $posts = get_posts(array(
    ‘posts_per_page’ => 1000,
    ‘post_type’ => ‘post’ // Replace post with the name of your post type
    ‘offset’ => 1000 // Increase this by 1000 each time.
    ));
    foreach($posts as $p){
    // Checks if Rank Math keyword already exists and only updates if it doesn’t have it
    $rank_math_keyword = get_post_meta( $p->ID, ‘rank_math_focus_keyword’, true );
    if ( ! $rank_math_keyword ){
    update_post_meta($p->ID,’rank_math_focus_keyword’,strtolower(get_the_title($p->ID)));
    }
    }
    }
    add_action( ‘init’, ‘update_focus_keywords’ );

    BUT, i relieve this error:

    Your PHP code changes were rolled back due to an error on line 10 of file wp-content/themes/Newspaper/rank-math.php. Please fix and try saving again.
    syntax error, unexpected ”offset” (T_CONSTANT_ENCAPSED_STRING), expecting ‘)’

    And another question, when I add all the focuswords to all my posts, what code should I leave in rank-math.php?

    Hello,

    There is a missing comma after the post type inside the array which is causing this issue. After adding the comma the code should run fine on the website provided you have the resources to run this for 1000 posts each time.

    You should also increase the offset each time the code runs to update all the posts.

    Don’t hesitate to get in touch if you have any other questions.

    Thanks I solved it….. But creates a incompatibility issue with TagDiv Newspaper Theme (fyi)

    <?php
    /**
    * Function to automatically update the focus keyword with the post title, if no focus keyword is set
    */
    function update_focus_keywords()
    {
    $posts = get_posts(array(
    ‘posts_per_page’ => 1000,
    ‘post_type’ => ‘post’, // Replace post with the name of your post type
    ‘offset’ => 1000 // Increase this by 1000 each time.
    ));
    foreach($posts as $p){
    // Checks if Rank Math keyword already exists and only updates if it doesn’t have it
    $rank_math_keyword = get_post_meta( $p->ID, ‘rank_math_focus_keyword’, true );
    if ( ! $rank_math_keyword ){
    update_post_meta($p->ID,’rank_math_focus_keyword’,strtolower(get_the_title($p->ID)));
    }
    }
    }
    add_action( ‘init’, ‘update_focus_keywords’ );

    Hello,

    Can you please share more details about the incompatibility issue you are facing with our keyword automation code and your current theme?

    Please share the error message you see on your end if there’s any so we can further understand the issue and provide a solution/advice accordingly.

    Looking forward to helping you.

    Its weird, now the code is not working, its possible get the correct one? to copy paste and then run the test again?

    Hello,

    Can you please confirm if there are any error messages shown after applying the code on your website?

    If there aren’t any, we might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Hello, here the code:

    <?php
    /**
    * Function to automatically update the focus keyword with the post tags, if no focus keyword is set
    */
    function update_focus_keywords()
    {
    $posts = get_posts(array(
    ‘posts_per_page’ => 1000,
    ‘post_type’ => ‘post’ //replace post with the name of your post type
           ‘offset’ => 1000 // Increase this by 1000 each time.
    ));
    foreach ($posts as $p) {
    $keywords = [];
    if (get_the_tags($p->ID)) {
    foreach ((get_the_tags($p->ID)) as $tag) {
    $keywords[] = strtolower($tag->name);
    }
    if (!get_post_meta($p->ID, ‘rank_math_focus_keyword’, true)) {
    update_post_meta($p->ID, ‘rank_math_focus_keyword’, implode(“, “, array_unique($keywords)));
    }
    }
    }
    }
    add_action(‘init’, ‘update_focus_keywords’);

    And here the error:

    Your PHP code changes were rolled back due to an error on line 10 of file wp-content/themes/Newspaper/rank-math.php. Please fix and try saving again.
    syntax error, unexpected identifier ” “, expecting “)”

    Hello,

    Can you please try this filter code instead and run it again?

    function update_focus_keywords(){
    	$posts = get_posts(array(
    		'posts_per_page' => 1000,
    		'post_type' => 'post' //replace post with the name of your post type
    		'offset' => 1000 // Increase this by 1000 each time.
    	));
    	foreach ($posts as $p) {
    		$keywords = [];
    		if (get_the_tags($p->ID)) {
    			foreach ((get_the_tags($p->ID)) as $tag) {
    				$keywords[] = strtolower($tag->name);
    			}
    			if (!get_post_meta($p->ID, 'rank_math_focus_keyword', true)) {
    				update_post_meta($p->ID, 'rank_math_focus_keyword', implode(',', array_unique($keywords)));
    			}
    		}
    	}
    }
    
    add_action('init', 'update_focus_keywords');

    Let us know how that goes. Looking forward to helping you.

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Hello,

    We apologize for the inconvenience.

    There’s a missing comma after the post_type array, please try this filter code again:

    function update_focus_keywords(){
    	$posts = get_posts(array(
    		'posts_per_page' => 1000,
    		'post_type' => 'post', //replace post with the name of your post type
    		'offset' => 1000 // Increase this by 1000 each time.
    	));
    	foreach ($posts as $p) {
    		$keywords = [];
    		if (get_the_tags($p->ID)) {
    			foreach ((get_the_tags($p->ID)) as $tag) {
    				$keywords[] = strtolower($tag->name);
    			}
    			if (!get_post_meta($p->ID, 'rank_math_focus_keyword', true)) {
    				update_post_meta($p->ID, 'rank_math_focus_keyword', implode(',', array_unique($keywords)));
    			}
    		}
    	}
    }
    
    add_action('init', 'update_focus_keywords');

    I have tested the code on my end and it should work now on your end too.

    Looking forward to helping you with this one.

    Hello, Thanks, seems is working but on WordPress failed but it worked with Cpanel (File Manager), just a last question, when I finished to increment by 1000 can I put the next code in order to keep updating with the new post/news?

    /**
    * Function to automatically update the focus keyword with the post title, if no focus keyword is set
    */
    function update_focus_keywords() {
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘post’ // Replace post with the name of your post type
    ));
    foreach($posts as $p){
    // Checks if Rank Math keyword already exists and only updates if it doesn’t have it
    $rank_math_keyword = get_post_meta( $p->ID, ‘rank_math_focus_keyword’, true );
    if ( ! $rank_math_keyword ){
    update_post_meta($p->ID,’rank_math_focus_keyword’,strtolower(get_the_title($p->ID)));
    }
    }
    }
    add_action( ‘init’, ‘update_focus_keywords’ );

    UPDATE; Sorry I double check the posts but are not updating the keywords,

    and the news posts are not getting keywords from tittle neither

    Hello,

    If your website resources cannot handle the automatic update of the focus keywords you should not put the code that updates all at the same time.

    You need to perform the update via the method we mentioned before of constantly adding and increasing the value but that won’t work for new posts.

    The only other solution would be to increase the server resources to a value that would allow the unlimited offset which is the first code you tried to add on the website.

    Don’t hesitate to get in touch if you have any other questions.

    But the incremental increasing by (1000) is not working. I applied the new Conde that you sent me, and nothing happens, I increment by 1000 and nothing happens.

    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.

Viewing 15 replies - 1 through 15 (of 16 total)

The ticket ‘Bulk Focus Keyword Issue with thousands of posts’ is closed to new replies.