• Resolved Brent Wilson

    (@bwbama)


    Hi,

    With Yoast, I have scripts pulling data in from APIs and then I update the new pages title and meta description in the following way:

    					update_post_meta($post->ID, '_yoast_wpseo_metadesc', $meta_desc);
    					update_post_meta($post->ID, '_yoast_wpseo_title', $meta_title);

    What is the equivalent for this SEO plugin?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    We have a method available in the the_seo_framework() object you can use: update_single_post_meta_item()

    That method takes care of all the sanitization and hooks, and it is also forward-compatible with planned changes.

    For example:
    the_seo_framework()->update_single_post_meta_item( '_genesis_title', $meta_title, $post );

    The the_seo_framework() object mitigates erroneous calls, so you can safely update the plugin today and worry about any API changes tomorrow. But, you must be sure the object is available.

    So, from your example, a stable piece of code would look like this:

    $tsf = function_exists( 'the_seo_framework' ) ? the_seo_framework() : null;
    
    if ( $tsf ) {
    	$tsf->update_single_post_meta_item( '_genesis_description', $meta_desc, $post );
    	$tsf->update_single_post_meta_item( '_genesis_title', $meta_title, $post );
    }

    I hope this helps ?? Cheers!

    Thread Starter Brent Wilson

    (@bwbama)

    Thank you, this works perfectly.

    Thread Starter Brent Wilson

    (@bwbama)

    Hi,

    I seem to be having issues with the Meta Description not showing up in pages until I save the post.

    Here is what I am doing:

    I create a new page, fill in my information. Save the page.

    I run a script on a cron or manually, that updates the Title and Description based on custom fields inside the post.

    After the script is run, I refresh the page. The title tag has updated correctly but the Description does not show in the source.

    If I edit the page, I can see that the Description is saved and shows in the page SEO box, however, the description will not show inside the source of the page until I hit the update button.

    Here is an example of the code in my cron:

    else if ( $post->current_office == 'Mayor' OR $post->votesmart_title == 'Mayor' ) {
    			if ( !empty($post->votesmart_parties) ) {
    				if ( $post->votesmart_parties == 'Democratic' ) {
    					$party_is = 'Democrat';
    				} else if ( $post->votesmart_parties == 'Republican' ) {
    					$party_is = 'Republican';
    				}
    				$has_party = ' is a ' . $party_is . ' and';
    			} else {
    				$has_party = '';
    			}
                $meta_desc = $post->post_title . $has_party . ' currently serves as the Mayor of ' . $post->votesmart_homeCity . ', Alabama.';
                if ( $post->_genesis_description != $meta_desc OR empty($post->_genesis_description) ) {
                    $tsf->update_single_post_meta_item( '_genesis_description', $meta_desc, $post->ID );
                }
                $meta_title = $post->post_title . ' - Mayor of ' . $post->votesmart_homeCity . ', AL';
                if ( $post->_genesis_title != $meta_title OR empty($post->_genesis_title) ) {
                    $tsf->update_single_post_meta_item( '_genesis_title', $meta_title, $post->ID );
                }
    }
    • This reply was modified 5 years, 1 month ago by Brent Wilson.
    • This reply was modified 5 years, 1 month ago by Brent Wilson.
    Plugin Author Sybre Waaijer

    (@cybr)

    Hello again!

    I think there might be an (object/database) caching plugin active, which only clears when you update a post. I suspect that when the cronjob runs, the cache has already been frozen with the old data.

    I can not think of another reason for why this happens. Therefore, details (such as a link to the affected pages) will be helpful.

    Thread Starter Brent Wilson

    (@bwbama)

    Hi,

    We do use Memcached through Memcached Redux

    An example URL where the description is not loaded: https://www.bamapolitics.com/alabama/alabama-government-officials/profiles/jackie-mitchell/

    Is there a way to clear the Meta Description cache for a post when updating via the cron script?

    • This reply was modified 5 years, 1 month ago by Brent Wilson.
    • This reply was modified 5 years, 1 month ago by Brent Wilson.
    Thread Starter Brent Wilson

    (@bwbama)

    I can confirm that restarting memcached clears the cache and the meta description. I’d rather not do this frequently though.

    Plugin Author Sybre Waaijer

    (@cybr)

    Have you perchance enabled “Enable object cache?” under “SEO Settings -> General -> Performance”?

    It’s redundant when you also have page-caching enabled. Disabling that feature may resolve the issue entirely.

    I’m also working on new filters with the next update, that’ll allow you to override the metadata the moment it’s being saved, mitigating the issue. Details will follow in the changelog.

    Thread Starter Brent Wilson

    (@bwbama)

    Yes, disabling the Object Cache setting fixed the issue. Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘update_post_meta like with Yoast’ is closed to new replies.