WP_Query::is_single()
Determines whether the query is for an existing single post.
Works for any post type excluding pages.
If the $post parameter is specified, this function will additionally check if the query is for one of the Posts specified.
Method of the class: WP_Query{}
No Hooks.
Return
true|false
. Whether the query is for an existing single post.
Usage
global $wp_query; $wp_query->is_single( $post );
- $post(int|string|int[]|string[])
- Post ID, title, slug, path, or array of such to check against.
Default: ''
Notes
- See: WP_Query::is_page()
- See: WP_Query::is_singular()
Changelog
Since 3.1.0 | Introduced. |
WP_Query::is_single() WP Query::is single code WP 6.7.2
public function is_single( $post = '' ) { if ( ! $this->is_single ) { return false; } if ( empty( $post ) ) { return true; } $post_obj = $this->get_queried_object(); if ( ! $post_obj ) { return false; } $post = array_map( 'strval', (array) $post ); if ( in_array( (string) $post_obj->ID, $post, true ) ) { return true; } elseif ( in_array( $post_obj->post_title, $post, true ) ) { return true; } elseif ( in_array( $post_obj->post_name, $post, true ) ) { return true; } else { foreach ( $post as $postpath ) { if ( ! strpos( $postpath, '/' ) ) { continue; } $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type ); if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) { return true; } } } return false; }