Check if post exists by slug in WordPress

/**
 * post_exists_by_slug.
 *
 * @return mixed boolean false if no post exists; post ID otherwise.
 */
function post_exists_by_slug( $post_slug ) {
	$args_posts = array(
		'post_type'      => 'post',
		'post_status'    => 'any',
		'name'           => $post_slug,
		'posts_per_page' => 1,
	);
	$loop_posts = new WP_Query( $args_posts );
	if ( ! $loop_posts->have_posts() ) {
		return false;
	} else {
		$loop_posts->the_post();
		return $loop_posts->post->ID;
	}
}

No comments yet.

Leave a Reply