Last active
August 29, 2015 14:16
-
-
Save joshuadavidnelson/af55611120067b011766 to your computer and use it in GitHub Desktop.
post type helper functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Remove post meta values. | |
* | |
* @since 1.0.0 | |
* | |
* @uses remove_post_meta() | |
* @uses ic3_log_me() | |
* | |
* @param int|array|object $posts Accepts either the post ID, post object, or an array of either | |
* @param string|array $meta The post meta to delete, either a single string or array of strings | |
* @return void | |
*/ | |
public function remove_meta( $posts, $metas ) { | |
if( is_array( $metas ) ) { | |
foreach( $metas as $meta ) { | |
remove_post_meta( $posts, $meta ); | |
} | |
} elseif( is_string( $metas ) ) { | |
remove_post_meta( $posts, $metas ); | |
} else { | |
ic3_log_me( 'Invalid meta type in remove_post_meta' ); | |
} | |
} | |
/** | |
* Remove a post meta value | |
* | |
* @since 1.0.0 | |
* | |
* @uses delete_post_meta() | |
* @uses ic3_log_me() | |
* | |
* @param int|array|object $posts Accepts either the post ID, post object, or an array of either | |
* @param string $meta The post meta to delete | |
* @return void | |
*/ | |
public function remove_post_meta( $posts, $meta ) { | |
if( is_array( $posts ) ) { | |
foreach( $posts as $post ) { | |
$success = false; | |
if( is_object( $post ) && isset( $post->ID ) ) { | |
$success = delete_post_meta( $post->ID, $meta ); | |
} elseif( is_numeric( $post ) ) { | |
$success = delete_post_meta( $post->ID, $meta ); | |
} else { | |
ic3_log_me( '' ); | |
} | |
if( !$success ) | |
ic3_log_me( "Unable to deleta meta: {$meta} from post {$post_id}" ); | |
} | |
} elseif( is_numeric( $posts ) || ( is_object( $posts ) && isset( $posts->ID ) ) ) { | |
if( is_numeric( $posts ) ) { | |
$post_id = intval( $posts ); | |
} elseif( is_object( $posts ) ) { | |
$post_id = $posts->ID; | |
} | |
$success = delete_post_meta( $post_id, $meta ); | |
if( !$success ) | |
ic3_log_me( "Unable to deleta meta: {$meta} from post {$post_id}" ); | |
} else { | |
ic3_log_me( 'Invalid post argument in remove_post_meta' ); | |
} | |
} | |
/** | |
* Set the post to a draft, useful on save | |
* | |
* @since 1.0.0 | |
* | |
* @uses ic3_log_me() | |
* @uses get_post_status() | |
* @uses wp_update_post() | |
* | |
* @param int $post_id The ID of the post to update | |
* @return boolean True on success, false on failure. | |
*/ | |
public function set_post_status( $post_id, $status = 'draft' ) { | |
$stati = get_post_stati(); | |
if( is_numeric( $post_id ) && is_string( $status ) && in_array( $status, $stati ) ) { | |
$post_id = intval( $post_id ); | |
} else { | |
return false; | |
} | |
// if it's not already a draft, make it so! | |
if( get_post_status( $post_id ) != $status ) { | |
$args = array( | |
'ID' => $post_id, | |
'post_status' => $status | |
); | |
$id = wp_update_post( $args ); | |
if( $id != 0 ) { | |
return true; | |
} else { | |
ic3_log_me( "Unable to update post {$post_id}" ); | |
return false; | |
} | |
} else { | |
ic3_log_me( 'Post status already set' ); | |
return false; | |
} | |
} | |
/** | |
* Remove the post's parent | |
* | |
* @since 1.0.0 | |
* | |
* @uses ic3_log_me() | |
* @uses wp_update_post() | |
* | |
* @param int|object $post The post object or post ID | |
* @return boolean True on success, false on failure. | |
*/ | |
public function remove_post_parent( $post ) { | |
if( is_numeric( $post ) ) { | |
$post_id = intval( $post ); | |
} elseif( is_object( $post ) && isset( $post->ID ) && isset( $post->post_parent ) && is_numeric( $post->post_parent ) ) { | |
$post_id = $post->ID; | |
$current_parent = $post->post_parent; | |
} else { | |
return false; | |
} | |
$args = array( | |
'ID' => $post_id, | |
'post_parent' => '' | |
); | |
$id = wp_update_post( $args ); | |
if( $id != 0 ) { | |
return true; | |
} else { | |
ic3_log_me( "Unable to update post parent for post {$post_id}" ); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment