• Resolved david6677

    (@david6677)


    Hello,

    Could you tell me please, what is the best way to add/update SEO metakeywords programmatically?

    i looked it up and found on this article how to update meta title and meta desc : https://ehikioya.com/how-to-update-yoast-seo-fields-programmatically/

    when i tried to update the meta key words with the same code it wont work.

    $updated_title = update_post_meta($post_id, ‘_yoast_wpseo_title’, $metatitle); //working
    $updated_desc = update_post_meta($post_id, ‘_yoast_wpseo_metadesc’, $metadesc); //working
    $updated_kw = update_post_meta($post_id, ‘_yoast_wpseo_metakeywords’, $metakeywords); // not working

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • @david6677

    After using update_post_meta, please follow it with an ‘wp_update_post’ call. The update post meta will update the post_meta table. The update_post will trigger an update to the Yoast indexable.

    Here is an example code snippet. As the code snippet is only an example, it does not have any restrictions and will run on every page load. This means you’ll want to adjust it to fit your situation so it only runs when necessary.

    Sorry for pitching in on this topic, but I have a related question, and I don’t want to explain this situation again.

    Anyway, I managed to update the meta description and focus keyword programmatically, but my problem it’s that the bullets from SEO Score and Readability won’t turn green in the posts columns list. However, in the single post edit screen, the bullets are green.

    My question would be, how can I trigger Yoast to also make green the bullets from the post columns list? I used the wp_update_post function right after updating the meta description and focus keyword, but it does not seem to work.

    Here it’s a screenshot to see this behavior in action and to better understand the problem.

    Is there another trick to this?

    Thanks so much.

    Thread Starter david6677

    (@david6677)

    If you update to the most current version of WordPress and Yoast SEO, does this resolve the issue?

    1. Check for conflicts.
    2. Check for JavaScript errors with your console.
    If you find any JavaScript errors related to Yoast SEO or if there is a conflict with a plugin or a theme, you can create a new GitHub issue for our developers. Please report the issue to a third party developer as well.`

    can you share the code that update the meta keywords and desc, there are couple of ways, i cant explain why but some works better then the other.

    Hi David.
    Thanks for replying.
    1. There are no conflicts
    2. There are no errors in the developer console

    I am running WordPress 5.9.2 and the last version of Yoast 18.3

    Here it’s the code I use to update:

    <?php
    //exit if accessed directly
    defined( 'ABSPATH' ) or exit;
    
    class Description_Yoast{
    	
    	function __construct( $post_ID ){
    		$this->post_ID = $post_ID;
    		add_action( 'wp', [ $this, 'desc' ]);
    	}
    	
    	function desc(){
    
    		$new_focus_kw = 'New focus keyword';
    		update_post_meta( $this->post_ID, '_yoast_wpseo_focuskw', $new_focus_kw );
    
    		$desc = 'Some description';
    		
    		$post = array(
    			  'ID'           => $this->post_ID,
    			  'post_content' => $desc,
    		);
    		
    		wp_update_post( $post );
    	}
    	
    }
    
    new Description_Yoast( $id );
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Add/update SEO metakeywords programmatically’ is closed to new replies.