WordPress conditional tags: is_single, is_singular, is_main_query, in_the_loop
Conditional tags are the functions starting with is_, in_ and has_ that tell a theme or a plugin what the current request is: a single post, an archive, a search, the admin. They read the global WP_Query, so they give correct answers only after WordPress has parsed the request. Most bugs with them come from three things: calling them too early, confusing similar tags, and forgetting that they describe the main query, not the query you are working with.
Problem: A condition like if ( is_single() ) is always false in one place, true where it should not be in another, and a filter on the_content also changes excerpts and widgets.
Solution: Check the conditions on the right hook, choose the tag that matches the context exactly, and inside query callbacks use the methods of the query object.
Timing
Conditional tags work after the main query is set up, i.e. from the wp action on. On init or directly in functions.php the query does not exist yet:
<?php
// WRONG: the query is not parsed yet, always false
add_action( 'init', function () {
if ( is_single() ) { /* never runs */ }
} );
// RIGHT: after the query
add_action( 'template_redirect', function () {
if ( is_single() ) { /* reliable */ }
} );
Similar tags that mean different things
<?php is_single(); // a single post of any post type, except pages and attachments is_single( 'hello' ); // that post (ID, slug or title) is_page(); // a single page is_singular(); // any single entry: post, page, attachment, custom post type is_singular( 'movie' ); // a single entry of the 'movie' post type is_home(); // the blog posts page (the front page only if it shows the latest posts) is_front_page(); // the front page, whatever it shows is_archive(); // any archive: category, tag, taxonomy, date, author, post type is_post_type_archive( 'event' ); // the archive of 'event' is_category(); // category archives is_tag(); // tag archives is_tax( 'genre' ); // archives of the custom taxonomy 'genre' (not categories or tags)
is_main_query() and the query methods
In a pre_get_posts callback the global tags describe the main query, but the query being built can be any other: a widget, a menu, a WP_Query in a template. So check the object you received:
<?php
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_post_type_archive( 'event' ) ) {
$query->set( 'posts_per_page', 20 );
}
} );
The same applies to your own loops: inside a new WP_Query() loop, is_single() still describes the page, use $my_query->is_singular() for the custom query.
in_the_loop() for content filters
the_content also runs for excerpts, widgets, page builders and REST responses. To change only the body of the post on its own page:
<?php
add_filter( 'the_content', function ( $content ) {
if ( is_singular( 'post' ) && in_the_loop() && is_main_query() ) {
$content .= '<p class="share-prompt">Share this article!</p>';
}
return $content;
} );
NOTE: is_admin() checks whether the request goes to the admin area, not whether the user is an administrator. It is also true for every request to admin-ajax.php, including AJAX calls from the front end. To tell them apart, add wp_doing_ajax(): if ( is_admin() && ! wp_doing_ajax() ). For the user’s rights use current_user_can().