I'm new to WordPress. What is the easiest way to trigger an email to admin when a user's search has no results.


You haven't provided much info so I will give you a basic example.

You need to find whatever template your theme is using to display the results, possibly search.php. If this isn't your theme create a child theme and duplicate the file that displays the results.

This file should already have an if statement for displaying the results, possibly an if/else. If not you will need to add one. The Else is used for when there are no results. You can grab what was entered in the search with get_search_query().

Inside the else you can use wp_mail() to send an email.

For Example:

if ( have_posts() ) : while ( have_posts() ) : the_post();
    // Code to display your results
    // No results so send an email
    $query = get_search_query();
    $to = 'adminEmail@mail.com';
    $subject = 'User Search';
    $message = 'User searched  ' . $query;
    wp_mail($to, $subject, $message );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.