Get posts ids by post title

Get posts ids by post title

function get_posts_by_title($title, $post_type = 'post') {
   global $wpdb;
   $posts_ids = array();
   $posts = $wpdb->get_results($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = '" . $post_type . "'", $title),     OBJECT);
   foreach ($posts as $post) {
       $posts_ids[] = $post->ID;
   }
   return $posts_ids;
}

Put this function in functions.php and can use in your code.

This function I use in project beatsbydre

 

 

 

Leave a Reply