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')";

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

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
  • Thanks. This works. Just a new issue that arose-the function gets executed multiple times, sometimes thrice, sometimes 4 times – bodesam Apr 26 '14 at 9:51
  • Never mind. Fixed – bodesam Apr 26 '14 at 10:13

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.