Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
If I have the workbench moderation module enabled and I would like to unpublish my content, how would I accomplish this?
I tried this but it didn't work:
$node = Node::load(1827);
$node->setPublished(FALSE);
$node->save();
Comments
Comment #2
haynescw CreditAttribution: haynescw commented$node = Node::load(1827);
$node->setPublished(FALSE);
$node->set('moderation_state', "draft");
$node->save();
But... I have just found a bug where published content that is returned to draft status is still visible to anonymous users. Working on trying to find a known issue, if not creating one.
Comment #3
haynescw CreditAttribution: haynescw at ICF commentedComment #4
albertski CreditAttribution: albertski at Xeno Media, Inc. commentedThanks this worked like a charm!
Comment #5
albertski CreditAttribution: albertski at Xeno Media, Inc. commentedComment #7
mohsinkhanmca CreditAttribution: mohsinkhanmca commentedHow to change user status?
Comment #8
CFG OCC CreditAttribution: CFG OCC commentedHere's an example of how content moderation was implemented on a migration POST_ROW_SAVE event on Drupal 8: -
Comment #9
sourabhutani CreditAttribution: sourabhutani at Kellton Tech Solutions Ltd commentedHi
Not working for me as i tried below code. I'm using workbench moderation 8.x-1.2 version.
$node = Node::load(1827);
$node->setPublished(FALSE);
$node->set('moderation_state', "draft");
$node->save();
Comment #10
imperator_99 CreditAttribution: imperator_99 commented@haynescw try setting the moderation state to 'archived', which will unpublish the revision (assuming you have an 'archived' state). The draft moderation state doesn't actually unpublish latest revision, ie:
Or, to do more than one:
Drupal::entityQuery()
can have conditions added to it to restrict the content types or anything else.Cheers,
Jesse.
Comment #11
avinash_thombre CreditAttribution: avinash_thombre commentedIf you want to unpublish the nodes in array of content types programmatically, use following code in any of your .intsall file inside hook_update:
function pfe_brand_update_7002(){
$cont_types = array('article', 'page');
foreach ($cont_types as $type) {
$cont_nodes = node_load_multiple(array(), array('type' => $type));
foreach ($cont_nodes as $cont_node) {
$cont_node->status = 0;
node_save($cont_node);
}
}
}
Comment #12
srutheesh CreditAttribution: srutheesh commentedDrupal 9:
Use following code
Unpublish :
Publish :