Skip to content

Instantly share code, notes, and snippets.

@lukecav
Created May 19, 2017 15:43
Show Gist options
  • Save lukecav/5b8c7e1027019e74686fa5bc983af7cb to your computer and use it in GitHub Desktop.
Save lukecav/5b8c7e1027019e74686fa5bc983af7cb to your computer and use it in GitHub Desktop.
Get Post by Title in WordPress
function get_post_by_title($page_title, $output = OBJECT) {
global $wpdb;
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='post'", $page_title ));
if ( $post )
return get_post($post, $output);
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment