Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuadavidnelson/8f8cdfe38d1aff53d5dd to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/8f8cdfe38d1aff53d5dd to your computer and use it in GitHub Desktop.
Force a post to draft if specific meta is invalid, 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, josh@joshuadnelson.com
*/
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 );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment