Meta query & tax query not working when altering search query.
-
The below code works on category archive pages, it filters out the necessary products. But on the search page it doesn’t work. If I uncomment the line //$query->set(“s”, “chelsea”);, then on the search page it does show the results for that search term. But the rest of the query, that is the meta query and tax query, are not working on the search page.
The website is built using Elementor and Crocoblock plugins.
//Alter query to exclude posts that are outdated, or whose category is hidden function mts_alter_query($query){ if(($query->get("post_type") === "product" && !is_admin() && !$query->is_singular()) || ($query->is_search() && !is_admin())){ //$query->set("s", "chelsea"); //Hide outdated posts $query->set("meta_key", "match-date"); $query->set("meta_value", date("Y-m-d")); $query->set("meta_compare", ">"); //Hide posts that belong to a hidden category $hidden_cat_ids = get_hidden_category_ids(); $original_tax_query = $query->get( 'tax_query', [] ); $new_tax_query = array( array( "taxonomy" => "product_cat", "field" => "term_id", "terms" => $hidden_cat_ids, "operator" => "NOT IN" ), $original_tax_query ); $query->set("tax_query", $new_tax_query); } }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Meta query & tax query not working when altering search query.’ is closed to new replies.