How to get page’s ID if I know the title only?

Is there an exact opposite funciton to this one:

get_the_title(ID) 

I know there’s:

get_the_id()

But it doesn’t seem to accept any arguments.

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

$title="Something";
get_the_id($title);

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?

4 Answers
4

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:

https://www.tumblr.com/erikt/278953342/get-a-wordpress-page-id-with-the-slug

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

G’luck!

Leave a Comment