0

I am trying to make search request from my custom form. For this I have an AJAX call on form submit:

jQuery("#header-search").on('submit', function () {
        jQuery.ajax({
            url: my_ajax_object.ajax_url,
            data: jQuery(this).serialize()
        })
        .then(function (res) {
            console.log(res)
        })
        return false
    })

In my functions.php I have:

function custom_search () {
    //WHAT SHOULD I CALL HERE
}
add_action( 'wp_ajax_nopriv_custom_search', 'custom_search' );
add_action( 'wp_ajax_custom_search', 'custom_search' );

I am not sure if this should be made as AJAX but I don't really understand what action should I put on the search form in the HTML. Everything seems like a mess for me. Can you assist me this? Appreciating every suggestion.

1 Answer 1

0

What I understood is that no AJAX is needed. If somebody don't understand all the fancy example around, you just need to use the pre_get_posts hook. In my case what I did is:

function custom_product_search($query) {
    $query->query_vars['post_parent'] = htmlentities($_GET['model']) ?? htmlentities($_GET['make']);
    return $query;
}
add_action("pre_get_posts", 'custom_product_search');

I don't know if this is the correct solution but it is working one.

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.