php - Modifying query in Wordpress search


I'm trying to integrate a Query Expansion software with Wordpress, but the wordpress search engine is an AND searcher (returns those contents that contain all the words in your query, and therefore the more extensive is the query the fewer results returns). For that reason when i use the query with the expansion features, the Searcher returns me less results. I would like to modify the Search's behavior, and turn it into something more like an OR searcher.

Example: For the query " iot, internet of things", I would want those posts which contents "iot", or "internet of things, or both of them.

I've reading through this document ( and i tried to use something like this:

add_action( 'template_redirect', 'hooks_setup' , 20 );
function hooks_setup() {
    if (! is_home() ) //<= you can also use any conditional tag here
    add_action( '__before_loop'     , 'set_my_query' );
    add_action( '__after_loop'      , 'set_my_query', 100 );

function set_my_query() {
    global $wp_query, $wp_the_query;
    switch ( current_filter() ) {
        case '__before_loop':
            //replace the current query by a custom query
            //Note : the initial query is stored in another global named $wp_the_query
            $wp_query = new WP_Query( array(
                'post_type'         => 'post',
                'post_status'       => 'publish',
               //others parameters...
            ) );

            //back to the initial WP query stored in $wp_the_query
            $wp_query = $wp_the_query;

But I don't know which parameters i need to set in wp_query. How can I achieve this?




To answer what parameters you need to set, you have to read through the same page you mentioned. Parameters for various items (post types or categories) are defined on this page and when you want to change it all you have to do is modify the parameters.

If you dont know what query you want to do I think its hard for people to help. If you are getting less results, you can open query a bit further or obtain results in 2 queries. Read through the page and you will find its common when people want to obtain different types of data and not everything is in the loop.

Also read through this to understand more about the loop and how to reset the query and do a new one then you can obtain two result sets and use them or combine them when and where you want.

Just to give you an example for string search in posts with s as parameter from the same page Prepending a term with a hyphen will exclude posts matching that term. Eg, 'pillow -sofa' will return posts containing 'pillow' but not 'sofa' (available since Version 4.4).

People are also looking for solutions to the problem: php - How to solve to ERROR: Could not able to execute INSERT INTO saveanswer (answer) VALUES ('d'). Field 'sanswer' doesn't have a default value


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.