Get WordPress Post ID from Post title

Get WordPress Post ID from Post title

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Get WordPress Post ID from Post Title

Retrieving a post ID directly from a post title string in PHP requires navigating around the core database schema, as there is no built-in get_post_id_where_title_is() function that accepts a simple string argument. The standard WordPress functions like get_page_by_title() or get_posts() are designed to query based on data currently stored in the database, not arbitrary strings passed from external sources or static references.

To solve this, you must construct a direct SQL query using $wpdb or utilize the global $wp_query object with specific arguments. The most robust approach involves querying the wp_posts table while ensuring you are checking only the post title and ignoring other fields that might contain similar strings.

Direct Database Query via $wpdb

The most performant method for a static lookup is to execute a raw SQL query against the wp_posts table. This bypasses WordPress's default filtering logic, which can be slow when querying large datasets.

You can use the global $wpdb object, which provides prepared statement wrappers that prevent SQL injection vulnerabilities even when dealing with user input or dynamic strings.

global $wpdb;

$target_title = 'foo'; // Your static reference string

// Prepare the query safely using %s for string placeholders
$query = $wpdb->prepare(
    "SELECT ID FROM {$wpdb->posts} 
     WHERE post_title = %s 
     LIMIT 1", 
    $target_title
);

$results = $wpdb->get_results($query, ARRAY_N); // Returns results as an array of arrays

if (!empty($results)) {
    // The first column in the result set is 'ID'
    $post_id = $results[0][0];
    
    // Now you can use this ID normally
    $post = get_post($post_id);
} else {
    // Handle the case where no post with that title exists
    // This prevents errors when trying to access null properties later
}

Important Implementation Details

When implementing this pattern, several technical nuances dictate success or failure. First, WordPress stores titles in a translatable format by default. If your theme supports multiple languages and the post was saved in a different language context, the post_title column might contain a slug or a translated version depending on your settings. However, for a simple static lookup where you control the input string, a direct equality check is usually sufficient.

Secondly, the LIMIT 1 clause is critical. If multiple posts share the exact same title (e.g., duplicate articles or drafts), this query returns only the first match based on the database index order. Without pagination logic, you cannot easily determine if there are duplicates. In scenarios where uniqueness is guaranteed, omitting the limit is safe but inefficient for large databases.

A common pitfall involves URL slugs versus actual titles. Developers often confuse post_name (slug) with post_title. The query above specifically targets post_title, which is the correct field for your requirement. If you were attempting to match a URL segment, you would need to query the wp_postmeta table linked to wp_term_relationships for taxonomy terms like categories, but that adds unnecessary complexity if you only have the title string.

Handling Special Characters

If the static reference string contains special characters or HTML entities, direct string matching in SQL behaves predictably as long as the input is properly escaped via $wpdb->prepare(). However, be cautious of whitespace. WordPress trims titles upon saving, but database storage retains the exact casing and spacing. If your static variable has leading spaces that the original post title does not, the query will return null. Always ensure the string being passed to the database query matches the stored value exactly, including case sensitivity.

If you find yourself needing to perform this lookup frequently in a high-traffic environment where the database is under load, consider creating a custom index on the post_title column if it does not already exist. This significantly speeds up equality searches, though be mindful of storage implications when adding indexes to large tables.

Our network