0

I am trying to use the pre_get_posts hook to replace WordPress's search function with my own, so I want to unset $query->query_vars['s'] so that it does not restrict the results, but I want to keep 's' as a property of $query so that I can use the get_search_query function. How would I go about doing this?

add_action('pre_get_posts', function (\WP_Query $query) {

        if (!$query->is_search()) {
            return;
        }
        if (is_admin()) {
            return;
        }
        $q = get_search_query();
        if (!$q) {
            return;
        }
        $search_parameters = array("q" => $q);
        $search_parameters = apply_filters('custom_search_parameters', $search_parameters);
        $search_results = custom_search($search_parameters);
        if ($search_results) {
            
            unset($query->query_vars['s']); // This causes get_search_query() to no longer work, but omitting it restricts the results.
            $query->query_vars['post__in'] = $search_results['post_ids'];
            $query->query_vars['orderby'] = 'post__in';
            $query = apply_filters('custom_search_query', $query);

        }
});
3
  • Does this answer your question? Change query variable for wordpress search
    – admcfajn
    Jan 26, 2022 at 22:18
  • @admcfajn No, that question is about how to change the url parameter from s to something else. I want to know how to get WP_Query to not filter on s if certain conditions are met but still keep it as a property of the query for use in other respects. Separate questions.
    – kloddant
    Jan 26, 2022 at 22:41
  • did you check the second answer? it seems to fit your description. realistically the answer is "no" it's not something that's advisable or likely to be maintainable.
    – admcfajn
    Jan 27, 2022 at 17:29

1 Answer 1

0

It would appear that I can hook into the get_search_query filter to restore the result of get_search_query() to what it was before I removed 's' from query_vars. This operates the way I want it to on the particular site that I am testing it on, but I am not sure if this s property is used in other ways that I have not considered, so even though this works in the current circumstance, I'd be glad to hear of alternatives that don't involve unsetting 's'.

add_action('pre_get_posts', function (\WP_Query $query) {

        if (!$query->is_search()) {
            return;
        }
        if (is_admin()) {
            return;
        }
        $q = get_search_query();
        if (!$q) {
            return;
        }
        $search_parameters = array("q" => $q);
        $search_parameters = apply_filters('custom_search_parameters', $search_parameters);
        $search_results = custom_search($search_parameters);
        if ($search_results) {
            
            unset($query->query_vars['s']);
            add_filter('get_search_query', function ($search) use ($q) {
                $search = $q;
                $search = urldecode($search);
                $search = strip_tags($search);
                $search = filter_var($search, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
                return $search;
            });
            $query->query_vars['post__in'] = $search_results['post_ids'];
            $query->query_vars['orderby'] = 'post__in';
            $query = apply_filters('custom_search_query', $query);

        }
});

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.