E
E
Elizaveta Krasova2021-02-26 19:03:08
WordPress
Elizaveta Krasova, 2021-02-26 19:03:08

How to sort AJAX search results correctly?

Hello.

We did an AJAX search for products on the site. We encountered such a problem that when entering a short word (for example, spruce), which is found in the names of other products, other products are displayed first, and only then only "Firs". The search was done on this lesson

For clarity of the situation.
Is it possible to first display products with a complete match at the beginning of the product, and only then all the rest?
60391aa867146541332006.png

How we are sampling now:

$arg = array(
    'post_type' => array('product'),
    'orderby'=> array('type'=>'DESC', 'title'=>'ASC'),
    'post_status' => 'publish',
    'product_cat' => $category,
    's' => convert_english_letters($_POST['s']),
    'meta_query' => array(
            array(
                'key' => '_stock_status',
                'value' => 'instock'
            )
        )
);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2021-02-27
@dimonchik2013

blues not only ate, but also drank (c)
the easiest way is to write to the author of the module or hire a freelancer

A
Alexander, 2021-02-27
@apisklov

Use the exact
search options

P
Pychev Anatoly, 2021-02-27
@pton

In general, there are several solutions, as always.
You need to make 2 queries to the database on the server. One - exclusively for a phrase starting with "Spruce", the other for searching for a phrase anywhere.
To do this, take a look at the workings and class arguments WP_Query. It has a hook posts_clauses_requestwhere you can tweak the relevant part of the request.
Another solution is to delete standalone SQL queries in the database. Then you will have full control in your hands, but you will have to deal with caching issues (this is optional)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question