is_404() -­­ WordPress Conditional Function

is_404() is a WordPress Function that checks if the page exists programatically.

Return

bool: It either returns true/false

Important:

Use it after the query has been parsed because it relies on $wp_query object's property ($wp_query->is_404()).

How to use:

Wrap it inside a hook like wp which triggers after the query has been parsed.

add_action ( 'wp', function() {
    if (is_404()) {
        // Your Code Here
    }
} );

Source: wp-includes/query.php

function is_404() {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_404();
}

Leave a Reply

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