Joshua David Nelson

Bullshit Free WordPress Development

  • Services
  • Code
  • About
  • Contact

Force a Post to Draft on Save

Force a post to draft if specific meta is invalid (or other reason), in this case the start and end date/time.

<?php
/**
* For a post to draft if specific meta is invalid, in this case the start and end date/time.
*
* @author Joshua David Nelson, [email protected]
*/
add_action( 'updated_post_meta', 'jdn_force_draft', 10, 2 );
function jdn_force_draft( $meta_id, $post_id ) {
if( get_post_type( $post_id ) == 'events' ) {
$start = get_post_meta( $post_id, 'event_start', true );
$end = get_post_meta( $post_id, 'event_end', true );
if( ( !$end || !$start ) || empty( $start ) || empty( $end ) ) {
// Update post 37
$my_post = array(
'ID' => $post_id,
'post_status' => 'draft'
);
// Update the post into the database
wp_update_post( $my_post );
}
}
}
view raw force-draft.php hosted with ❤ by GitHub

Source: https://gist.github.com/joshuadavidnelson/8f8cdfe38d1aff53d5dd

Tags: post meta, post save, post status

Previous Code:
Remove Genesis Taxonomy Meta Fields
Back to the Code Snippets
Next Code:
Pure CSS Parallax Page Header in Genesis
  • Twitter
  • RSS Feed URL

About Me

I'm a WordPress Engineer. I build sleek, custom websites with WordPress and Genesis.

See my services and my recent work.

Contact me to get your project started.

Gravity Forms Plugin for WordPress Fastest WordPress Hosting

Recent Posts

  • Using Font Awesome Icons for WooCommerce Grid / List Toggle
  • Disable Blog: WordPress Gone Blog-less
  • Category (Taxonomy) Dropdown Filtered By Post Type
  • Weather in WordPress with Dark Sky
  • Fixing Your Deprecated Widget Constructors in WordPress 4.3
  • Twitter
  • RSS Feed URL
  • Code Snippets
  • My Plugins
  • Make a Payment

© Joshua David Nelson | Hand-Forged | WordPress + Genesis | Terms of Service | Legal | Contact