5

I'm faced with the situation of needing to be able to publish content with Drush. Is there a way to use Drush to publish nodes? If there is, how can it be done?

2
  • Why do you need to publish with drush? How exactly do you want to input nodes through drush? And what kind of nodes are you expecting to generate? I mean, do you want to specify the title? The body? Empty nodes? Are you sure you need Drush? Maybe the Drupal API is enough - in this case see: drupal.org/node/889058 – Sifro Oct 15 '13 at 18:25
  • I don't need to generate content. Ideally in my scenario I'd like to be able to either target all nodes or a specific set of nodes by listing node ids. – Lester Peabody Oct 15 '13 at 18:43
5

I am not sure if that is available by default (found no result on searching), but one thing that you can do is declaring your own Drush command in 3 easy steps:

  1. Creating the Command File:

    ( Give the right name to the command file and place it into a folder that Drush will look, That name must end with .drush.inc (in our case it will be publish.drush.inc) and must be placed either in the folder of a custom module on your site (will work for that site if the module is enabled) or in the .drush folder in your server’s root directory).

  2. Declaring the Command Function: To declare a new Drush command you have to invoke hook_drush_command() in the file you created above and below the php opening tag. Example as below:

function publish_drush_command() {
$items = array();
$items['publish-content'] = array(
'description' => 'Publish content',
'aliases' => array('pc'),
'arguments' => array(
'type' => 'Nodes of this type will be published',
),
);
return $items;
}

Note: This command is declared in a file that has been placed in the .drush folder that resides in the VPS’s root folder.

Here we have declared a new command called publish-content (a new key in the $items array). The description and what’s included in the arguments key is what you will see when you run the drush help publish-content command. What’s actually helpful here is that you can also specify an alias (or shortcut) that can be run instead of the longer publish-content version. In this case, drush publish-content will equal drush pc.
3. Declare the Command Callback Function: Now you need to write the code that you want Drupal to perform when you run the custom command. The name of the function needs to be in the format: drush_command_name. (Note: dash in the command name becomes an underscore in the function name).

function drush_publish_content($type) {
  $nodes = node_load_multiple(array(), array('type' => $type));
  $count = 0;
  foreach ($nodes as $node) {
    if($node->status == 0) {
      $count++;
      $node->status = 1;
      node_save($node);
    }  
  }
  if($count > 0) {
    drush_print($count . " nodes of the " . $type . " content type have been successfully published.");
  }

  else {
    drush_print("No nodes of the " . $type . " content type were published.");
  }
}

This callback function takes as argument the machine name of the content type of all the nodes we want published. This gets passed as a command argument like so (Example: pulish the contents of blog content-type):

drush publish-content blogs

Drush will then print out how many nodes have been published (displaying only the number of nodes that have been in fact affected by the command).

References: https://www.digitalocean.com/community/articles/how-to-declare-your-own-drush-commands-with-your-vps
https://github.com/edvillan15/node-tools/blob/master/node_tools.drush.inc

2
2

Publish a node in Drupal 8

drush php-eval '$node = node_load(123); $node->setPublished(true); $node->save();'

Publish a node node in Drupal 7

drush php-eval '$node = node_load(123); $node->status = 1; node_save($node);’

Publish all the nodes of a specific content type in Drupal 8

drush php-eval '$nodes = node_load_multiple(array(), array("type" => "here_the_type_name"));foreach($nodes as $node){ $node->setPublished(true); $node->save();}'

Publish all the nodes of a specific type in Drupal 7

drush php-eval '$nodes = node_load_multiple(array(), array("type" => "here_the_type_name")); foreach($nodes as $node){  $node->status = 1; node_save($node);}'
1

For drupal 8, this worked (via Dave Burns),

drush php-eval '$node = node_load(2); $node->setPublished(true); $node->save();'
1

In drupal 8 this can also be done via node name if the node id is unknown for some reason:

drush php-eval '$node=reset(\Drupal::entityTypeManager()->getStorage("node")->loadByProperties(["title"=>"Test Page"]));$node->setPublished(true);$node->save();'
1

Also in drupal 8, if you are using the core Content Moderation Module, replace:

$node->setPublished(true);

with

$node->set("moderation_state","published");

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.