• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Programmatically Update the Status of a Post Using Gutenberg JS

Wordpress · November 26, 2022

In a WordPress project I am building, I needed a way to programmatically update the post status of a post on the edit screen, specifically in the Gutenberg editor.

Before arriving at the solution below, I first struggled to figure this out. I was trying to call the updatePost method with a post object and modified status, then calling savePost but what kept happening was the post would revert to draft status.

It turns out that to update a post status (and other post-specific values), you need to call editPost first followed by savePost right after saving the changes.

wp.data.dispatch('core/editor').editPost({
    status: 'publish'
});

wp.data.dispatch('core/editor').savePost();

It seems so simple in hindsight, but it took me a while to work this out. If you are reading this, chances are you got stuck on this simple task too.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Thoughts on the Flipper Zero
  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • How To Paginate An Array In Javascript
  • Waiting for an Element to Exist With JavaScript
  • Reliably waiting for network responses in Playwright
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • How To Calculate A Javascript Date X Months Ago With Vanilla Javascript

Recent Comments

  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die
  • Dwayne on How to Create a Blockchain With TypeScript
  • kevmeister68 on PHP Will Not Die

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz