I am trying to create a save search notification system whereby a user can search certain parameters on my site and save the search query like Ebay does or other classified ad websites do. I understand the save search side of things, I have created a database table that contains the search_id, the search_name, the search_parameter and the user_id (Please let me know if this the correct method to go about doing this. I'm open to new ideas..) Anyway, I understand that side of things to a certain degree but the side I don't understand how it works is the notification side of things, how do I begin to check if new posts have been added that match that certain query and how do I notify a user once that has happened? Any advice and possible coding help would be greatly appreciated! (I'm kind of a coding newbie.)

Btw, I still don't know how to fetch the search parameter as it doesn't sow up in the url it's kind of done behind the scenes by ajax. So any help regarding that would also be appreciated.

Here is the code that I have so far:

The PHP:

function save_this_search() {
global $wpdb;

 $my_user_id = get_current_user_id();
 $search_name = $_REQUEST['search_name'];
 $search_parameter = $_GET['search_parameter'];

$count = $wpdb->get_var( "SELECT COUNT(*) FROM search_queries" );

$id = $count + 1;

'search_id' => $id,
'search_name' => $search_name,
'search_parameter' => 'test',
'user_id' => $my_user_id,

    array( '%s', '%s', '%s', '%s' )

/*echo 'The search name: '. $search_name;
echo 'My user ID: '. $my_user_id;
echo 'Id: '. $id;*/

add_action('wp_ajax_save_this_search', 'save_this_search');
add_action('wp_ajax_nopriv_save_this_search', 'save_this_search');

The Jquery AJAX:

$('.save_search_form').submit(function(e) {

var search_name = $('.save-search-input').val();

    type: "POST",
    url: ajaxurl,
    data: {
        action : 'save_this_search',
        search_name: search_name
    beforeSend: function() {
      $('save-search-input').prop('disabled', true);  
    success: function(data) {
        $('save-search-input').prop('disabled', false);
        //$('#savesearchmodal .modal-body').text('Search saved successfully! You will be notified when new ads are created matching your search criteria.');

And finally the HTML:

 <form class="save_search_form">
 <input class="save-search-input" placeholder="Name this search" type="text" name="save_search_input" />
<button type="submit" class="save-search-submit-button">Save</button>
  • For the notification part, you need to set up a cron job - a server cron is typically best because WP's cron only runs when you have active visitors on the site - and however often, such as daily or weekly, hit a PHP file that will run the search and trigger an email if there are results. – WebElaine Aug 1 '18 at 18:09
  • @WebElaine Right that sounds about right, only problem is I have no idea how to write that lol. Any ideas or starter tips regarding code? – Matt H Aug 1 '18 at 18:17

Your Answer

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

Browse other questions tagged or ask your own question.