Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ravinsharma12345/7939718 to your computer and use it in GitHub Desktop.
Save ravinsharma12345/7939718 to your computer and use it in GitHub Desktop.
/**
* Determine if a post exists based on post_name and post_type
*
* @param $post_name string unique post name
* @param $post_type string post type (defaults to 'post')
*/
function post_exists( $post_name, $post_type='post' ) {
global $wpdb;
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
$args = array();
if ( !empty ( $post_name ) ) {
$query .= " AND post_name LIKE '%s' ";
$args[] = $post_name;
}
if ( !empty ( $post_type ) ) {
$query .= " AND post_type = '%s' ";
$args[] = $post_type;
}
if ( !empty ( $args ) )
return $wpdb->get_var( $wpdb->prepare($query, $args) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment