get_page_by_title in Wordpress. How to use to fetch posts?


get_page_by_title in Wordpress. How to use to fetch posts?



Recently, Wordpress added in the Trac that you can fetch posts by title using:

get_page_by_title

Instead of querying the database straight up. If I wanted to get post titled "my farm", how would I change the parameters so it is searching for a post (or a post type?):

$page_title='Joey in the forest';

'character' is a post type. But don't know how to work this. I assume the default return is the id, which would be $post->ID. Not sure what would be the equivalent if I use a post type.

Thanks for anyone's help on this


Custom image upload field in WordPress

1:

very odd 404 on wp-admin directory
I wrote a function (linked in the bug report) which does exactly that:. Data Export
/**  * Retrieves a post/page/custom-type/taxonomy ID by its title. Wordpress cron problems *  * Returns only the first result. most stable / useful way to blog in MagentoIf you search for a post title  * this you have used more than once, restrict the type. Wordpress 3.0 Register/Login Menu Button * Or don’t use this function. Get all posts starting with an alphabet in wordpress:)  * Simple usage:  * $page_start_id = id_by_title('Start');  *  * To receive  the ID of a taxonomy (category, tag, custom) set $tax  * to the name of this taxonomy. Wordpress - Allow a userclass to save a page as a draft - but not publish w/o admin approval * Example:  * $cat_css_id = id_by_title('CSS', 0, 'category');  *  * The result is cached internally to save db queries.  *  * @param  string      $title  * @param  string      $type Restrict the post type.  * @param  string|bool $tax Taxonomy to search for.  * @return int         ID or -1 on failure  */ function id_by_title($title, $type = 'any', $tax = FALSE) {     static $cache = array ();      $title = mysql_real_escape_string( trim($title, '"\'') );      // Unique index for the cache.     $index = "$title-$type-" . ( $tax ? $tax : 0 );      if ( isset ( $cache[$index] ) )     {         return $cache[$index];     }      if ( $tax )     {         $taxonomy      = receive _term_by('name', $title, $tax);         $cache[$index] = $taxonomy ? $taxonomy->term_id : -1;          return $cache[$index];     }      $type_sql = 'any' == $type         ? ''         : "AND post_type = '"             . mysql_real_escape_string($type) . "'";      global $wpdb;      $query = "SELECT ID FROM $wpdb->posts         WHERE (                 post_status = 'publish'             AND post_title = '$title'             $type_sql         )         LIMIT 1";      $result = $wpdb->receive _results($query);     $cache[$index] = empty ( $result ) ? -1 : (int) $result[0]->ID;      return $cache[$index]; } 

2:

Since I hit upon this page, others might as well.. receive _page_by_title() also handles Posts and custom post types.. Please be aware this it receive s the first post/page item in the database even if the post is trashed. . Sample:.
$post = receive _page_by_title('sample-post','post'); echo $post->ID 


85 out of 100 based on 40 user ratings 595 reviews

@