Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created July 29, 2014 11:59
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
[WordPress] Using a helper function to write a lightweight, easy-to-read way to check if a WordPress post exists given an ID.
<?php
/**
* See: http://codex.wordpress.org/Function_Reference/get_post_status
* for more information.
*/
if ( FALSE === get_post_status( $id ) ) {
// The post does not exist
} else {
// The post exists
}
<?php
/**
* Determines if a post, identified by the specified ID, exist
* within the WordPress database.
*
* Note that this function uses the 'acme_' prefix to serve as an
* example for how to use the function within a theme. If this were
* to be within a class, then the prefix would not be necessary.
*
* @param int $id The ID of the post to check
* @return bool True if the post exists; otherwise, false.
* @since 1.0.0
*/
function acme_post_exists( $id ) {
return is_string( get_post_status( $id ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment