I create several nodes in my manifest and there are a few that I don't want in my sitemap.

I could not find a function that worked for me so I wrote the following function:

function _exclude_from_sitemap($nid) {
return db_update('xmlsitemap')
->fields(array(
'status' => 0,
'status_override' => 1,
))
->condition('loc', 'node/'.$nid)
->execute();
}

Is there another (better) way?

Comments

ngocketit’s picture

This may also work:

  $node = node_load($nid);
  $link = xmlsitemap_node_create_link($node);
  $link['status']  = 0;
  $link['status_override'] = 1;
  xmlsitemap_link_save($link, array($link['type'] => $node));
Aritra Banerjee’s picture

Hi folks, better hook for removing element without db query is hook_xmlsitemap_link_alter. I am providing one sample code where element is removed by checking the node id.

<?php

/**
 * Alter the data of a sitemap link before the link is saved.
 *
 * @param array $link
 *   An array with the data of the sitemap link.
 * @param array $context
 *   An optional context array containing data related to the link.
 */
function hook_xmlsitemap_link_alter(array &$link, array $context) {
  // Get node/[:id] from loc.
  $node_id = $link['loc'];
  // Condition to check for a particular nid.
  if ($node_id == 'node/[:id]') {  // [:id] is the desired nid to remove.
    // Status is set to zero to exclude the item in the sitemap.
    $link['status'] = 0;
    // Set to zero to make the element non-accessible by the anonymous user.
    $link['access'] = 0;
  }
  }
}
?>
renatog’s picture

Status: Active » Reviewed & tested by the community

Hi guys.

Thank you very much for your examples @ngocketit and @aritra-banerjee.

It's ok.

Good Work and Good Week.

Regards.

renatog’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

SKAUGHT’s picture

+1