1

I have a content type called article. For this content type, I unchecked the "Published" checkbox under the publishing options tab.

I have 30000 nodes I changed to unpublished. Among those, I want to publish 900 records. I have the node URLs.

Using the path aliases, how to get the node IDs? Once I have the node IDs, how can I publish those nodes?

2

You can use drupal_get_normal_path() to get the node/id-paths for each of them.

Then you can use the following code to publish them.

// Load the node object.
$node = node_load($nid);
// Set the status property to 1.
$node->status = 1;
// Re-save the node.
node_save($node);
  • How to save status change only? If we give node_save($node) then all the changes get saved. I want status change only get saved. – steniya Aug 23 '17 at 11:16
  • not sure I get the question, you only change the status value here, so that the one that's changed in the new saved object right? – Pan Chrono Aug 24 '17 at 11:27
1

If you have the path alias, you can easily get the normal path and then the node object with two single functions. I assume you have the path alias available in the $path_alias variable.

$path = drupal_get_normal_path($path_alias);
$node = menu_get_object('node', 1, $path);

Once you have the node object, you just set $node->status to NODE_PUBLISHED, and save the node. So the full code becomes the following.

$path = drupal_get_normal_path($path_alias);
if ($node = menu_get_object('node', 1, $path)) {
  $node->status = NODE_PUBLISHED;
  node_save($node);
}

I am not sure how much safe it's assuming the path aliases are for nodes, but just to be sure, and avoid trying to load a node when Drupal didn't find any normal path matching the path alias, I would use the following code.

$path = drupal_get_normal_path($path_alias);
if (strpos($path, 'node/') === 0 && ($node = menu_get_object('node', 1, $path))) {
  $node->status = NODE_PUBLISHED;
  node_save($node);
}

Notice that === is necessary, since strpos() can either return 0 to mean the path starts with 'node/', or FALSE to mean 'node/' was not found in the path. (In PHP 0 == FALSE is TRUE.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.