As you dive deeper into WordPress and begin working with theme customization and plugin development, you’ll quickly come across the need to find a page ID by title. The page ID simply acts as a unique identifier for each page in your WordPress database. This ID is needed when you want to target specific pages, perhaps to execute certain actions or control behavior. You might also need this ID when you have some conditional logic you want to implement, such as showing a banner on specific pages only. Additionally, many plugins and external tools require the page ID for certain functionalities like scheduling or content back ups. 

Thanks WordPress for All of Your Built-In Functions

Jumping right into it, one of the easiest programmatic ways to get a page ID by title is using WordPress’ built-in function: get_page_by_title. This function returns the entire page object, from which you can then pull out the page’s ID. 

// Function to get page ID by title
function get_page_id_by_title( $page_title ) {

  $page = get_page_by_title( $page_title, OBJECT, 'page' ); // Set post type to 'page'

  if ( $page ) {
    return $page->ID; // This is pulling out just the ID from the page object
  } else {
    return null; // Return null if page not found
  }
}

Here’s what’s happening in the above snippet of code:

  1. First, we simply defined a function get_page_id_by_title that takes a page title as an argument, 
  2. Inside of the function, we use the built-in WordPress function get_page_by_title. This function returns the page object based on the page title passed in.
  3. If the page is found, we pull out the ID and return it; otherwise we return null.

The Trusty WP_Query

Using the built-in get_page_by_title function is super easy and efficient. However, it doesn’t provide you with much flexibility. If you require more customization, you can use the wp_query function. Here are some examples of when you might choose to use wp_query to get a page id:

  • When You Have Conditional Logic with Multiple Criteria

For example, you might want to retrieve page IDs based on title and tag. The wp_query function in this case allows you to get the page based on both criteria.

  • When You Need More than the ID and Basic Page Object

Wp_query has an advantage over get_page_by_title because you can get back more than just the basic page object such as total post count, categories, and more.

Here’s an example of how to get page ID by title and category:

function get_page_id_by_title_and_category( $page_title, $category_slug ) {

  $args = array(
    'post_type' => 'page', // Specify post type as 'page'
    'name' => $page_title, // Search by page title (using 'name' parameter)
    'tax_query' => array(
      array(
        'taxonomy' => 'category', // Target the 'category' taxonomy
        'field' => 'slug', // Search by category slug (using 'slug' parameter)
        'terms' => array( $category_slug ), // Specify the desired category slug
      ),
    ),
    'posts_per_page' => 1, // Limit results to 1 (since we only need the ID)
  );

  $query = new WP_Query( $args );

  if ( $query->have_posts() ) {
    while ( $query->the_post() ) {
      return get_the_ID(); // Get the ID of the found page
    }
  } else {
    return null; // Return null if no page found
  }

  wp_reset_postdata(); // Reset post data after the loop
}

Here’s what’s happening in the above snippet:

  1. First we defined a function get_page_id_by_title_and_category that takes in page title and category as arguments.
  2. Inside of the function, we created an array $args to define all of the query parameters:
  3. Then we set $query equal to a new wp_query object that has the $args as a parameter
  4. We then check to see if $query has any posts. If it does, we enter a loop.
  5. Inside the loop, we simply get the ID of the found page and return it. 
  6. Finally, we use wp_reset_postdata() to reset post data to avoid affecting any future queries. 

Diving into The Database

Another option that is fast and direct is to simply query the database. This isn’t best practice or the most highly recommended approach, and I’d only recommend it if it’s a one off and you’re familiar with DB queries. If you need page IDs on a regular basis, definitely choose one of the other options above. But if you’re still set on diving into the DB, you can simply write a query like this:

SELECT ID FROM wp_posts WHERE post_title = 'Page Title' AND post_type = 'page’

This will return the page ID based on the ‘Page Title’ you pass in.

And There’s Always Plugins and the URL

One quick and easy way to get a page ID is to simply use a plugin. This is a great option for folks who don’t want to deal with the hassle of writing code. Luckily, there are quite a few plugin options to choose from. 

And finally, you can simply look at the URL of a page

/wp-admin/post.php?post=41 //41 is the ID

As you can tell, you’ve got many options when it comes to finding a page ID by title. For everyday use, wp_query offers the most flexibility and programmatic control. The less complicated options may be all you need though. No need to over-complicate things if it isn’t needed. YAGNI – you ain't gonna need it!