0

I'm trying to write a function that will 'grab' the search term entered into the blog search form, and then hook the function to the get_search_query filter like this add_filter('get_search_query','myFunction'); so that everytime a search is made, 'myFunction' runs and grabs the search term (or so I think), I have this line in myFunction to grab the search term

$search_term = get_search_query();

but it seems to be causing problem, the blog doesn't display, except I comment that particular line out. What is the right way to achieve the subject. Thanks.

Here is the code:

function myFunction(){
global $wpdb;
$search_term = get_search_query();
$table = $wpdb->prefix . "tableName"; 
$insert = "INSERT into $table(`serach_term`) VALUES ('$search_term')";
$wpdb->query($insert);   
}

add_filter('get_search_query','myFunction');
4
  • 1
    Do you have debugging enabled? Please post all of the relevant code.
    – s_ha_dum
    Commented Apr 25, 2014 at 12:28
  • Edit that code into the question please.
    – s_ha_dum
    Commented Apr 26, 2014 at 1:08
  • And the debugging information
    – s_ha_dum
    Commented Apr 26, 2014 at 1:18
  • I enabled debugging but I didn't get any message or logs
    – bodesam
    Commented Apr 26, 2014 at 1:20

1 Answer 1

1

You're adding a filter to get_search_query, and within that function calling get_search_query, which runs your filter, which calls get_search_query, which runs your filter, which calls get_search_query, which runs your filter… do you see the problem here?

The search query is passed to the filter as an argument, so you don't need to fetch it-

function myFunction($search_term){
    // now you can use $search_term directly in your code
}
add_filter('get_search_query','myFunction');
2
  • Thanks. This works. Just a new issue that arose-the function gets executed multiple times, sometimes thrice, sometimes 4 times
    – bodesam
    Commented Apr 26, 2014 at 9:51
  • Never mind. Fixed
    – bodesam
    Commented Apr 26, 2014 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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