Last updated on September 25, 2023

Customize the “No Results Found” Message in Search

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Customize "No Results" message.

To customize the “No Results Found” message in the WordPress search, you can use the pre_get_posts action hook to modify the search query and check if there are any results. If there are no results, you can then modify the search query to display a custom message.

Here’s an example code snippet that demonstrates how to achieve this functionality:

function wpsnippets_custom_no_results_message( $query ) {
    if ( $query->is_search() && $query->is_main_query() && $query->is_empty() ) {
        // Modify the search query to display a custom message
        $query->set( 's', 'No results found. Please try again.' );
    }
}
add_action( 'pre_get_posts', 'wpsnippets_custom_no_results_message' );

In this code snippet, we’re using the pre_get_posts action hook to modify the search query. We first check if the current query is a search query ($query->is_search()), the main query ($query->is_main_query()), and if the search query is empty ($query->is_empty()). If all these conditions are met, we modify the search query using the set() method of the $query object. In this example, we’re setting the search query to a custom message: “No results found. Please try again.”

You can customize the message to fit your needs by modifying the string passed to the set() method.

This code snippet can be useful when you want to provide a more informative and user-friendly message to your visitors when they perform a search that returns no results. Instead of the default “No Results Found” message, you can display a custom message that guides users on what to do next or provides additional information.

Examples

Example 1: Customize the “No Results Found” message in search

This use case demonstrates how to customize the “No Results Found” message that is displayed when a search query returns no results. By using the pre_get_posts action hook, we can modify the search query and check if any results are found. If not, we can customize the message to provide a more meaningful response to the user.

function wpsnippets_custom_no_results_message( $query ) {
    if ( $query->is_search() && $query->is_main_query() && $query->found_posts == 0 ) {
        $query->set( 'no_found_rows', true );
        $query->set( 's', '' ); // Clear the search query
        $query->set( 'post_type', 'page' ); // Set the post type to 'page'
    }
}
add_action( 'pre_get_posts', 'wpsnippets_custom_no_results_message' );

function wpsnippets_custom_no_results_message_text( $message ) {
    if ( is_search() && ! have_posts() ) {
        $message = 'No results found. Please try a different search term.';
    }
    return $message;
}
add_filter( 'get_search_form', 'wpsnippets_custom_no_results_message_text' );

The code above first hooks into the pre_get_posts action to modify the search query. It checks if the current query is a search query, the main query, and if no results are found. If these conditions are met, it modifies the query to search only for pages and clears the search query.

Next, it hooks into the get_search_form filter to customize the “No Results Found” message. It checks if the current page is a search page and if no posts are found. If these conditions are met, it modifies the message to display a custom text.

Example 2: Customize the “No Results Found” message for a specific post type

This use case demonstrates how to customize the “No Results Found” message for a specific post type. By modifying the get_search_form filter, we can check the current post type and customize the message accordingly.

function wpsnippets_custom_no_results_message_text( $message ) {
    if ( is_search() && ! have_posts() && get_post_type() === 'product' ) {
        $message = 'No products found. Please try a different search term.';
    }
    return $message;
}
add_filter( 'get_search_form', 'wpsnippets_custom_no_results_message_text' );

The code above hooks into the get_search_form filter to customize the “No Results Found” message. It checks if the current page is a search page, if no posts are found, and if the current post type is ‘product’. If these conditions are met, it modifies the message to display a custom text specifically for the ‘product’ post type.

Example 3: Customize the “No Results Found” message with HTML markup

This use case demonstrates how to customize the “No Results Found” message with HTML markup. By modifying the get_search_form filter, we can include HTML tags and styling to enhance the appearance of the message.

function wpsnippets_custom_no_results_message_text( $message ) {
    if ( is_search() && ! have_posts() ) {
        $message = '<p class="no-results">No results found. Please try a different search term.</p>';
    }
    return $message;
}
add_filter( 'get_search_form', 'wpsnippets_custom_no_results_message_text' );

The code above hooks into the get_search_form filter to customize the “No Results Found” message. It checks if the current page is a search page and if no posts are found. If these conditions are met, it modifies the message to include an HTML paragraph element with a custom CSS class for styling purposes.

Last updated on September 25, 2023. Originally posted on October 4, 2023.

Leave a Reply

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