Using WordPress ‘is_single()’ PHP function

The is_single() WordPress PHP function determines whether the query is for an existing single post.

Usage

is_single($post);

Parameters

  • $post (int|string|int|string) (Optional) – Post ID, title, slug, or array of such to check against. Default: ”

More information

See WordPress Developer Resources: is_single()

Examples

Check if it’s a single post page

This code snippet checks if the current page is a single post page.

if (is_single()) {
  // Run code only for single post-type 'post'
}

Check if it’s a specific single post by ID

This code snippet checks if the current page is a single post with a specific ID.

if (is_single(17)) {
  // Run code for Post 17 (ID)
}

Check if it’s a specific single post by title

This code snippet checks if the current page is a single post with a specific title.

if (is_single('Irish Stew')) {
  // Run code for post_title of "Irish Stew"
}

Check if it’s a specific single post by slug

This code snippet checks if the current page is a single post with a specific slug.

if (is_single('beef-stew')) {
  // Run code for post_name (slug) of "beef-stew"
}

Check if it’s a specific single post by array of values

This code snippet checks if the current page is a single post that matches an array of ID, slug, or title.

if (is_single(array(17, 'beef-stew', 'Irish Stew'))) {
  // Run code for single post with ID 17, post_name "beef-stew", or post_title "Irish Stew"
}