+44 (0)785 715 1348
office@bogdans.co.uk

WordPress – get post IT by title 0

Posted on 11, September 2013

in Category Web development


It returns the post id based on the post title. It may not work if multiple posts have the same title..

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;
}

0 Comments