WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an exact opposite funciton to this one:


I know there's:


But it doesn't seem to accept any arguments.

So, basically, I'm looking for something like:

$title = 'Something';

I already know this solution:

 global $wpdb;
 $post_name = get_query_var('name');
 $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = $post_name");

But maybe there's something bulit-in that I'm missing?

share|improve this question
How are you getting the title? – imHavoc Jul 28 '11 at 22:40
[shortcode title="User inserts title here"] – Wordpressor Jul 28 '11 at 22:48
up vote 3 down vote accepted

I'm not sure how to get it via the title, but you can get it via the slug (which is often more useful in my experience) using this:


Just change "$page" to "$post" if you want to return slugs for posts instead of pages.


share|improve this answer
Note: this will not work for sub-pages (pages that are children of other pages). – montrealist Apr 8 '13 at 15:49

get_page_by_title( $title, [$output = 'object'], [$post_type = 'page'] ) exists for this very purpose. I'm using it in one of my projects now to display all attachments for a particular page in my sidebar:

if ( $oPage = get_page_by_title('Proudly Supporting...') ) {

  $children = get_posts(array(
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'numberposts'    => 3,
    'post_status'    => null,
    'post_parent'    => $oPage->ID, /* Using the Page ID */
    'orderby'        => 'rand',

  if ( $children ) {
    /* Do something with the children */

share|improve this answer

I had the same problem. I used the next code:

$my_title = 'My title';
global $wpdb;
$mypost = $wpdb->get_row( "SELECT * FROM wp_posts WHERE post_title = '" . $my_title . "' " );
echo $mypost->ID;
share|improve this answer

There are a built in function to do it. Use: get_page_by_title();

See the referrence here http://codex.wordpress.org/Function_Reference/get_page_by_title

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.