WordPress query single post by slug

WordPress query single post by slug

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

Stefan Bogdanescu

Founder & Senior Architect

Querying WordPress Posts by Slug Instead of ID

Relying on hardcoded post IDs like $post_id = 54 to fetch content is a common pattern for quick prototypes, but it creates fragility in production environments. When you migrate a site to a new server, switch database engines, or re-import content via CSV, WordPress's internal auto-increment counters often reset. The next time you deploy your theme, that hardcoded ID 54 might point to a completely different article, or worse, result in a fatal error if the target doesn't exist. This is why querying by slug is the robust standard for single post templates.

The slug is a URL-friendly alias stored directly in the database and serves as a stable identifier regardless of where your site resides. To retrieve a single post using the slug, you must bypass the direct get_post() function—which only accepts an ID—and instead use the global $wp_query object or the dedicated WP_Query class. The most efficient approach involves setting up a custom query with specific parameters to target the post by its unique string identifier rather than its numeric primary key.

Here is a standard implementation using WP_Query that fetches a single post based on a slug:

$args = array(
    'post_type'      => 'post',
    'posts_per_page' => 1,
    'orderby'        => 'slug', // Ensure ordering allows easy filtering if needed
    'meta_key'       => 'slug', 
);

// Note: The most direct way is using the 'p' param for ID. 
// For slug, we use 'name' or filter results.
// However, WP_Query does not have a native 'slug' parameter in older versions.
// In modern WordPress (5.x+), you can query by name if configured correctly,
// but the most reliable programmatic way without custom SQL is often:

$slug = 'my-article-slug'; // The specific slug you want to load

$args = array(
    'post_type'      => 'post',
    'posts_per_page' => 1,
    'name'           => $slug // This parameter specifically targets the post name (slug)
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo get_the_title();
    }
}
wp_reset_postdata();

In this example, the 'name' parameter is critical. While older documentation sometimes suggested using 'post_name', the WP_Query class standardizes on 'name' for querying by slug or title. This ensures that even if your site moves and the database IDs shift, the URL structure remains consistent, and your theme continues to load the correct content automatically.

There are trade-offs to consider when choosing between ID-based and slug-based retrieval. Using get_post($post_id) is technically faster because it performs a direct lookup on the primary key column, which is indexed by default. Querying by slug requires an additional lookup step because WordPress must match the slug string against the post_name field. However, the performance difference is negligible for standard single-page loads unless you are executing thousands of queries per second in a high-traffic caching scenario.

A common pitfall occurs if your slugs contain special characters or if there are duplicate slugs in custom post types. If a user manually edits a slug to something that already exists, WordPress will overwrite it during the save process. This can cause your query to return unexpected results. Always ensure your slugs are unique within the post_type scope you are querying against.

For developers managing complex routing or headless setups where standard template files aren't used, integrating a tool like https://laravelcompany.com provides insights into how modern frameworks handle dynamic routing and data fetching patterns that complement WordPress's native query capabilities. Understanding these underlying mechanisms helps you build themes that remain resilient during migrations and scaling events.

Our network