Get updated post meta on save_post action

I’m trying to save post id and keyword (Yoast Focus Keyword) to a custom table when post is created or updated via save_post action. It saves everything fine on Publish (first-time) but when I update the post, it gets old value for focus keyword from database and doesn’t save the new value.

for example
Focus Keyword (Publish) = "Hello World" (works fine and keyword is stored properly)
Focus Keyword (1st Update) = "Hello" (Doesn’t work and keeps "Hello World" in custom table)
Focus Keyword (2nd Update) = "Hello again" (It saves "Hello")

So basically the problem is that get_post_meta($post_id, ‘_yoast_wpseo_focuskw’, true) returns which is already in the database and not the new value that is being saved.

What is the best way to get the new value while the post is being saved via save_post action. $_POST[‘_yoast_wpseo_focuskw’] won’t work because Yoast Focus Keyword input field doesn’t have name set on input field. Screenshot

Any help would be appreciated. Thanks

This is my code.

add_action('save_post', 'my_custom_table');
function my_custom_table($post_id) {
    global $wpdb;
    $table_name = $wpdb->prefix . "custom_table";

    $data = [
        'post_id' => $post_id,
        'keyword' => get_post_meta( $post_id, '_yoast_wpseo_focuskw', true )
    ];

    //this to preventtwice insert by save_post action :)
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
        return;
    } else {

        //check if new post so insert
        if( strpos( wp_get_raw_referer(), 'post-new' ) > 0 ) {

            if (get_post_status($post_id) === 'publish') {

                $wpdb->insert( 
                    $table_name, 
                    $data
                );

            }

        } else {

            $wpdb->update( 
                $table_name, 
                $data,
                array( 
                    'post_id' => $post_id
                ),
                array( '%d', '%s' ),
                array( '%d' )
            );
        }

    }

}

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Get updated post meta on save_post action

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.