How to override priority and change frequency in Yoast xml sitemap

It is important to maintain healthy xml sitemap for your site. In case you are using Yoast SEO plugin to generate xml sitemap for a wordpress site, it provides some hooks to override priority and change frequency of various urls.

wordpress-xml-sitemap

Here is wordpress code snippet to customize xml sitemap generated by Yoast seo plugin.

add_filter('wpseo_sitemap_entry', 'my_sitemap_priority', 10, 3);
function my_sitemap_priority($url, $sitemap_type, $contextobj) {
  $newurl = $url;
  // tag sitemap
  if ($sitemap_type == 'term') {
    // change frequency
    // replace this with your logic
    $newurl['chf'] = 'daily';
    // priority
    // replace this with your logic
    if ($url['pri'] <= 0.6) {
      $newurl['pri'] += 0.2;
    }
  } else if (in_array($sitemap_type, array('post', 'page'))) {
    // replace these with your logic
    $newurl['chf'] = 'daily';
    $newurl['pri'] = 0.8;
  }
  return $newurl;
}

Few points to note

  1. $contextobj contains either $post or $tag object.
  2. The default priority and frequency should work in most cases. You should customize these only if you know what you are doing.
  3. Max priority value can be 1.0 and these values are only relative for all urls on your site.
  4. For more information you may want to see file class-sitemaps.php in your plugin directory.
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments