Skip to main content

Unpublish Feature

Summary

The Unpublish Feature automates the process of unpublishing posts based on a user-defined date/time. It adds:

  • Admin Writing Section:
    A new section under Settings > Writing that allows users to select which post types (CPTs) will support scheduled unpublishing. This selection activates the "Unpublish Date" functionality for the chosen CPTs.

  • Publish Metabox Integration:
    For posts belonging to the selected CPTs, a new Unpublish Date field is added to the Publish metabox. This field uses an ACF date_time_picker to let users specify the date and time at which the post should be automatically unpublished (i.e., switched to draft mode).

    • The field converts the local date/time input to GMT for storage, ensuring consistent scheduling across timezones.
    • When displayed on the edit page, the stored GMT value is converted back to the site’s local time.
  • Scheduled Actions:
    A scheduled Action Scheduler event (Yoko\UnpublishPost) is set up to check for posts with an unpublish date that has passed, and then unpublish them accordingly.

    • The feature validates that the unpublish date is not in the past and, if it is, displays an admin warning notice indicating that unpublishing cannot be scheduled on a past date.

Requirements

  • ACF – Advanced Custom Fields plugin
  • Action Scheduler

Testing Guide

  1. Select CPTs:

    • Go to Settings > Writing and select the CPT(s) you want to enable for scheduled unpublishing.
  2. Verify Field Appearance:

    • Open the edit screen of a published post belonging to one of the selected CPTs.
    • Ensure that the Unpublish Date field appears in the Publish metabox.
  3. Set Unpublish Date (Valid):

    • Update a published post by setting the Unpublish Date to a future date and time.
    • Verify that the field converts the local date/time to GMT for storage and that it correctly converts back to local time when displayed.
    • Confirm in the database that the value is stored in GMT.
  4. Set Unpublish Date (Invalid):

    • Attempt to set the Unpublish Date to a past date.
    • Confirm that no unpublish date is saved and that a warning notice appears stating:
      "The unpublish date was not updated because you cannot schedule unpublishing on a past date."
  5. Run Scheduled Action:

    • Navigate to Tools > Scheduled Actions.
    • Filter by Pending.
    • Locate the scheduled action Yoko\UnpublishPost and click Run.
    • Verify that the published post with a valid unpublish date is switched to draft mode.

Additional Notes and Troubleshooting

  • The feature won't work without ACF enabled.
  • The unpublish date is stored in GMT in the database for consistency and is converted to local time when displayed.
  • Ensure your WordPress timezone (under Settings > General) is set correctly.
  • The feaure logs scheduled unpublishing actions with yoko_log at yoko-logs/yoko-unpublish-post-yyyy-mm-dd.log
  • If a post is not unpublished as expected, check the logs and the scheduled actions at wp-admin/tools.php?page=action-scheduler.

Screenshots

Screenshot 1

Screenshot 2