• Resolved jorensm

    (@jorensm)


    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)
  • Moderator bcworkz

    (@bcworkz)

    The meta query part appears to be fine, it’s how you’re trying to combine taxonomy query args that’s the issue. Setting the existing tax query to default [] is not an acceptable part of a “tax_query” arg. Not all taxonomy queries use “tax_query”, category and tag queries could use their own set of query vars. Older code might even use the taxonomy name as a query var instead of “tax_query” (deprecated method).

    If you were to remove $original_tax_query from the “tax_query” array, I’d expect your search query to work properly. If you must incorporate existing taxonomy criteria as part of your query, you’ll need a more sophisticated way of incorporating any possible form of taxonomy args, and correctly omitting the arg array when there is none.

    Hint: use the Query Monitor plugin to evaluate the effect your code has on the eventual SQL. When you see 0=1 in SQL, it means the WP query parser couldn’t make sense of what it was given, as would happen when passed an empty array for “tax_query”.

    Thread Starter jorensm

    (@jorensm)

    @bcworkz The issue turned out to be related to a plugin that was altering the query. I turned it off, and now it works fine (even with the current way the tax query is set)

    • This reply was modified 1 year, 11 months ago by jorensm.
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.