Drupal 7 xmlsitemap set priority and change frequency

  Hi, sometimes we need to do something extra, especially when we need to make some SEO. In drupal we have wonderfull xmlsitemap module. So sometimes an output of this module is not very good for us. We need to change priorities and change frequencies. Good news for us is that to set priority we can use administration interface, but frequency can be set only for front page. Some taxonomy pages also could be changed frequently but xmlsitemap module don't use it. Example we have parent taxonomy term page and some child terms has been added to the parent. It means that parent was changed, but actual parent term was not changed. Xmlsitemap module also don't know about various views which is changed frequently. 

  So to set priority and change frequency could be used code given below. There we trick xmlsitemap module and set change priority and set change frequency to weekly on all listed content type nodes. If we need to set priority to some taxonomy pages or any menu link we also can do it.

  How can we find appropriate id to update it in hook_xmlsitemap_link_alter()? By default for node it is node id, for taxonomy term - taxonomy term id, and for menu link - menu link id. We can find them all in xmlsitemap table. That's all, thank you.

/**
 * Implements hook_xmlsitemap_link_alter().
 *
 * We need it to implement different settings to content types
 * links and taxonomies.
 */
function yourmodule_xmlsitemap_link_alter(&$link) {
  //Content types.
  $content_types = array(
    'article',
    'gallery',
    'video',
    'event',
  );

  if (in_array($link['subtype'], $content_types)) {
    $link['priority'] = 0.7;
      $link['lastmod'] = time() - 84600 * rand(1, 6);
      $link['changefreq'] = XMLSITEMAP_FREQUENCY_WEEKLY;
  }
  elseif($link['type'] == 'taxonomy_term') {
    //We need it to prevent xmlsitemap code xmlsitemap.generate.inc lines 193-200
    //Last modified yesterday.
    $link['lastmod'] = time() - 3600 * rand(1, 12);
    // Some taxonomy pages ids
    $terms = array( 1, 2, 3, 4, 5);

    $channels = array(4,11,39);
    if (in_array($link['id'], $terms)) {
      $link['priority'] = 0.9;
      $link['changefreq'] = XMLSITEMAP_FREQUENCY_DAILY;
    }
  }
  elseif ($link['type'] == 'menu_link') {
    $menu_link = array(1, 2, 3, 4, 5);

    if (in_array($link['id'], $menu_link)) {
      //We need it to prevent xmlsitemap code xmlsitemap.generate.inc lines 193-200
      //Last modified yesterday.
      $link['lastmod'] = time() - 3600 * rand(1, 12);
      $link['priority'] = 0.8;
      $link['changefreq'] = XMLSITEMAP_FREQUENCY_DAILY;
    }
  }
}

/* If we need to regenerate sitemap programmatically 
   use hook_update_N() implementation.
   You sould use it in yourmodule.install file. */

/**
 * Implements hook_update_N();
 *
 * Regenerate xmlsitemap sitemap.xml file.
 */

function yourmodule_update_7001() {
  // Regenerate sitemap.xml.
  module_load_include('generate.inc', 'xmlsitemap');
  $form_state['values']['entities'] = array(
    'menu_link',
    'node',
    'taxonomy_term',
  );
  $form_state['values']['save_custom'] = 1;
  $batch = xmlsitemap_rebuild_batch($form_state['values']['entities'], $form_state['values']['save_custom']);
  batch_set($batch);
}