Modifying the publish date meta data in the Yoast SEO WordPress Plugin

Yoast is one of the most famous WordPress SEO Plugins and a rich featured toolset. However its neither trivial nor well documented how to change certain outputs of it. ready2order is publishing blog articles on certain topics which are updated on a regular basis. However in search results e.g. by Google the publishing date of blog articles is used:

This causes old articles to look outdated (which is also a bad thing for SEO and traffic) even if they are way more up-to-date. Simply changing the publish date in WordPress however is also not an option, as it would destroy the blog order and structure. Our solution is to modify the meta publish date to be the last modified date without touching the real publish date of the article. We are hooking into the Yoast Plugin like this:

// define the wpseo_opengraph_show_publish_date callback
function r2o_overwrite_yoast_publish_date( $content ) {
    $date = new DateTime(get_post_modified_time('c',true));
    $date->setTimezone(new DateTimeZone(get_option('timezone_string')));
    return $date->format('c');
};

// add the filter
add_filter( 'wpseo_og_article_published_time', 'r2o_overwrite_yoast_publish_date', 90, 2 );

If you are in a similar situation, we hope this will help you too.


Categories

Tags