Last active
August 29, 2015 14:19
-
-
Save maxxscho/aa0d244a1ae59d8b96fc to your computer and use it in GitHub Desktop.
Wordpress get post by slug and post type
This file contains hidden or 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
/** | |
* 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