Using is_single() in WordPress: A Complete Guide with Real Examples [[year]]

The is_single() function is one of those classic WordPress conditionals that every developer uses — but not everyone fully understands.

In this tutorial, we’ll break down exactly what is_single() does, how to use it properly, and when it might trip you up. As always — no fluff, just code and clarity.

What is is_single() in WordPress?

is_single() is a conditional tag used to check whether a single post (of any post type that is publicly queryable) is being displayed. It returns true if a single post is being viewed, and false otherwise.

Basic usage:

if ( is_single() ) {
    // Do something
}

 How It Works

This function works only on the front-end (not in the admin area), and only during the main query — so using it outside the Loop or inside AJAX/admin calls won’t return what you expect.

Practical Examples

Run code on all single post pages

if ( is_single() ) {
    echo '<p>This is a single post.</p>';
}

Target a specific post by slug

if ( is_single( 'hello-world' ) ) {
    // Code only runs on the post with slug "hello-world"
}

Target by post ID

if ( is_single( 42 ) ) {
    // Runs only on the post with ID 42
}

Target by post title (rarely used)

if ( is_single( 'My First Post' ) ) {
    // Less reliable due to formatting
}

Multiple targets

if ( is_single( array( 42, 'hello-world', 'Another Post Title' ) ) ) {
    // Match by ID, slug, or title
}

Important Notes

  • is_single() only works for single post views — not pages, not archives, and not custom post types unless they are set as publicly queryable.
  • To check if you’re on a page, use is_page().
  • For custom post types, make sure they are set with 'public' => true in register_post_type().

SEO Use Case Example

Let’s say you want to add structured data or custom meta tags to a specific blog post:

if ( is_single( 'seo-guide' ) ) {
  echo '<meta name="description" content="A complete SEO guide for WordPress." />';
}

Perfect for fine-tuning SEO per post without needing a plugin.

Developer Tip

Always check is_single() inside the wp action or later to ensure WordPress has finished building the query.

add_action( 'wp', function() {
  if ( is_single() ) {
    // Safe to run your logic here
  }
});

🔗 Reference

Official Docs: is_single() – WordPress Developer Resources

Final Thoughts

is_single() is a powerful yet simple tool for customizing your templates and functionality. Whether you’re inserting custom elements or controlling code logic, it’s essential in every WordPress dev’s toolbox.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *