Friday, December 9, 2022
HomeWordPress DevelopmentSearch question alteration not working for meta values

Search question alteration not working for meta values


The bellow code works on class archive pages, it filters out the required merchandise. However on the search web page it does not work. If I uncomment the road //$query->set("s", "chelsea");, then on the search web page it does present the outcomes for that search time period. However the remainder of the question, that’s the meta question and tax question, should not engaged on the search web page.

That is my question alteration code:

//Alter question to exclude posts which can be outdated, or whose class is hidden
perform mts_alter_query($question){

    if(($query->get("post_type") === "product" && !is_admin() && !$query->is_singular()) || ($query->is_search() && !is_admin())){

        //$query->set("s", "chelsea");
        //Cover outdated posts
        $query->set("meta_key", "match-date");
        $query->set("meta_value", date("Y-m-d"));
        $query->set("meta_compare", ">");

        //Cover posts that belong to a hidden class

        $hidden_cat_ids = get_hidden_category_ids();

        $original_tax_query = $query->get( 'tax_query', [] );

        $query->set("tax_query", array(
            array(
                "taxonomy" => "product_cat",
                "area" => "term_id",
                    "phrases" => $hidden_cat_ids,
                    "operator" => "NOT IN"
                ),
            $original_tax_query
        ));
    }

}
add_action( 'pre_get_posts', 'mts_alter_query', 999);

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments