Skip to content

Instantly share code, notes, and snippets.

@maxxscho
Last active August 29, 2015 14:19
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 maxxscho/aa0d244a1ae59d8b96fc to your computer and use it in GitHub Desktop.
Save maxxscho/aa0d244a1ae59d8b96fc to your computer and use it in GitHub Desktop.
Wordpress get post by slug and post type
/**
* Retrieve by its slug
* @param string $slug Slug of the post
* @param string $post_type Post Type
* @param string $output Type of output
* @return mixed
* @author Markus Schober
*/
function bb_get_post_by_slug($slug, $post_type = 'post', $output = 'OBJECT') {
global $wpdb;
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $slug, $post_type ) );
if ( $post ) {
return get_post( $post, $output );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment