0

I recently installed ElasticPress on my woocommerce store, and it's working fine -- the product page is showing normally, and the search bar is working fine with the new elastic capabilities. However, when I try to use the product filters, nothing shows up, the query simply doesn't work. If I deactivate Elasticpress, however, the filters work normally. This is the query:

<?php

defined( 'ABSPATH' ) || exit;

global $wp_query;

add_action( 'woocommerce_product_query', 'tpk_archive_query' );

function tpk_archive_query( $q ){

    if($_GET['js'] == 'y') {
        $cat = explode(',', esc_attr($_GET['categorias']));
        $a = explode(',', esc_attr($_GET['artistas']));
        $ta = explode(',', esc_attr($_GET['tamanhos']));
        $c = explode(',', esc_attr($_GET['colecoes']));
        $t = explode(',', esc_attr($_GET['tags']));
        $preco = explode(',', esc_attr($_GET['preco']));
        $operador = strtoupper(esc_attr($_GET['op']));
        $orderby = explode('-', esc_attr($_GET['orderby']));
    } else {
        $cat = esc_attr($_GET['cat']);
        $a = esc_attr($_GET['a']);
        $ta = esc_attr($_GET['ta']);
        $c = esc_attr($_GET['c']);
        $t = esc_attr($_GET['t']);
    }



    if((esc_attr($_GET['orderby']) && !empty(esc_attr($_GET['orderby'])))) {

        if($orderby[0] == 'preco') {

            $q->set('orderby', 'meta_value_num');
            $q->set('meta_key', '_price');
            $q->set('order', $orderby[1]);

        } else {

            $q->set('orderby', $orderby[0]);
            $q->set('order', $orderby[1]);

        }

    }



    $args = [
        'relation' => 'AND',
    ];

    if((esc_attr($_GET['cat']) && !empty(esc_attr($_GET['cat'])))) {
        
        $category = [
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => $cat,
            'operator' => 'IN'
        ];

        array_push($args, $category);
    } elseif(esc_attr($_GET['categorias']) && !empty(esc_attr($_GET['categorias']))) {

        if(in_array('kits', $cat)) {

            $categorias = ['relation' => 'OR',];

            foreach ($cat as $cate) {

                if($cate != 'kits') {

                    $kitCat = 'kits-' . $cate;

                    $cate = [$cate, $kitCat];

                    $pima = [
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => $cate,
                        'operator' => 'IN'
                    ];
                    
                    array_push($categorias, $pima);

                }

            }

        } else {

            $categorias = [
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $cat,
                'operator' => 'IN'
            ];

        }

        array_push($args, $categorias);

    }

    if((esc_attr($_GET['a']) && !empty(esc_attr($_GET['a']))) || (esc_attr($_GET['artistas']) && !empty(esc_attr($_GET['artistas'])))) {
        $artistas = [
            'taxonomy' => 'autor',
            'field' => 'slug',
            'terms' => $a,
            'operator' => 'IN'
        ];

        array_push($args, $artistas);
    }

    if((esc_attr($_GET['ta']) && !empty(esc_attr($_GET['ta']))) || (esc_attr($_GET['tamanhos']) && !empty(esc_attr($_GET['tamanhos'])))) {
        $tamanho = [
            'taxonomy' => 'tamanho',
            'field' => 'slug',
            'terms' => $ta,
            'operator' => 'IN'
        ];

        array_push($args, $tamanho);
    }

    if((esc_attr($_GET['c']) && !empty(esc_attr($_GET['c']))) || (esc_attr($_GET['colecoes']) && !empty(esc_attr($_GET['colecoes'])))) {
        $colecao = [
            'taxonomy' => 'colecao',
            'field' => 'slug',
            'terms' => $c,
            'operator' => 'IN'
        ];

        array_push($args, $colecao);
    }

    if((esc_attr($_GET['t']) && !empty(esc_attr($_GET['t']))) || (esc_attr($_GET['tags']) && !empty(esc_attr($_GET['tags'])))) {
        
        if(!$operador) {
            $operador = 'OR';
        }
        $tags = ['relation' => $operador,];

        foreach ($t as $tag) {
            $poma = [
                'taxonomy' => 'product_tag',
                'field' => 'slug',
                'terms' => $tag,
                'operator' => 'IN'
            ];
            
            array_push($tags, $poma);
        }

        array_push($args, $tags);
    }

    $q->set('tax_query', $args);

    if((esc_attr($_GET['preco']) && !empty(esc_attr($_GET['preco'])))) {

        $min_price = $preco[0];
        $max_price = $preco[1];

        $q->set('meta_query', [
            [
                'key' => '_price',
                'value' => [$min_price, $max_price],
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
            ]
        ]);

    }

}

I tried using WP Marvelous Debug to see if there were any errors happening during the query, and it didn't point at anything different. The only "errors" it pointed out were these undefined indexes, which I don't belive have anything to do with the problem, because they happen with or without Elastic.

This is the "Query Args" on the ElasticPress debugger:

array(72) {
["orderby"]=>
string(28) "meta.total_sales.double date"
["post_type"]=>
string(7) "product"
["error"]=>
string(0) ""
["m"]=>
string(0) ""
["p"]=>
int(0)
["post_parent"]=>
string(0) ""
["subpost"]=>
string(0) ""
["subpost_id"]=>
string(0) ""
["attachment"]=>
string(0) ""
["attachment_id"]=>
int(0)
["name"]=>
string(0) ""
["pagename"]=>
string(0) ""
["page_id"]=>
int(0)
["second"]=>
string(0) ""
["minute"]=>
string(0) ""
["hour"]=>
string(0) ""
["day"]=>
int(0)
["monthnum"]=>
int(0)
["year"]=>
int(0)
["w"]=>
int(0)
["category_name"]=>
string(0) ""
["tag"]=>
string(0) ""
["cat"]=>
string(0) ""
["tag_id"]=>
string(0) ""
["author"]=>
string(0) ""
["author_name"]=>
string(0) ""
["feed"]=>
string(0) ""
["tb"]=>
string(0) ""
["paged"]=>
int(0)
["meta_key"]=>
string(0) ""
["meta_value"]=>
string(0) ""
["preview"]=>
string(0) ""
["s"]=>
string(0) ""
["sentence"]=>
string(0) ""
["title"]=>
string(0) ""
["fields"]=>
string(0) ""
["menu_order"]=>
string(0) ""
["embed"]=>
string(0) ""
["category__in"]=>
array(0) {
}
["category__not_in"]=>
array(0) {
}
["category__and"]=>
array(0) {
}
["post__in"]=>
array(0) {
}
["post__not_in"]=>
array(0) {
}
["post_name__in"]=>
array(0) {
}
["tag__in"]=>
array(0) {
}
["tag__not_in"]=>
array(0) {
}
["tag__and"]=>
array(0) {
}
["tag_slug__in"]=>
array(0) {
}
["tag_slug__and"]=>
array(0) {
}
["post_parent__in"]=>
array(0) {
}
["post_parent__not_in"]=>
array(0) {
}
["author__in"]=>
array(0) {
}
["author__not_in"]=>
array(0) {
}
["order"]=>
string(4) "DESC"
["meta_query"]=>
array(0) {
}
["tax_query"]=>
array(2) {
    ["relation"]=>
    string(3) "AND"
    [0]=>
    array(4) {
    ["taxonomy"]=>
    string(5) "autor"
    ["field"]=>
    string(4) "slug"
    ["terms"]=>
    array(1) {
        [0]=>
        string(12) "arcane-minis"
    }
    ["operator"]=>
    string(2) "IN"
    }
}
["wc_query"]=>
string(13) "product_query"
["posts_per_page"]=>
int(48)
["ep_integrate"]=>
bool(true)
["ep_facet"]=>
bool(true)
["aggs"]=>
array(3) {
    ["name"]=>
    string(5) "terms"
    ["use-filter"]=>
    bool(true)
    ["aggs"]=>
    array(12) {
    ["category"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(19) "terms.category.slug"
        }
    }
    ["post_tag"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(19) "terms.post_tag.slug"
        }
    }
    ["post_format"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(22) "terms.post_format.slug"
        }
    }
    ["product_cat"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(22) "terms.product_cat.slug"
        }
    }
    ["product_tag"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(22) "terms.product_tag.slug"
        }
    }
    ["product_shipping_class"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(33) "terms.product_shipping_class.slug"
        }
    }
    ["autor"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(16) "terms.autor.slug"
        }
    }
    ["colecao"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(18) "terms.colecao.slug"
        }
    }
    ["tamanho"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(18) "terms.tamanho.slug"
        }
    }
    ["ep_custom_result"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(27) "terms.ep_custom_result.slug"
        }
    }
    ["pa_material"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(22) "terms.pa_material.slug"
        }
    }
    ["pa_variacao"]=>
    array(1) {
        ["terms"]=>
        array(2) {
        ["size"]=>
        int(10000)
        ["field"]=>
        string(22) "terms.pa_variacao.slug"
        }
    }
    }
}
["suppress_filters"]=>
bool(false)
["ignore_sticky_posts"]=>
bool(false)
["cache_results"]=>
bool(true)
["update_post_term_cache"]=>
bool(true)
["lazy_load_term_meta"]=>
bool(true)
["update_post_meta_cache"]=>
bool(true)
["nopaging"]=>
bool(false)
["comments_per_page"]=>
string(2) "30"
["no_found_rows"]=>
bool(false)
["taxonomy"]=>
string(5) "autor"
["term"]=>
string(12) "arcane-minis"
}

Do I have to re-write my queries in an elastic-specific syntax? I spent the last two days trying to fix this and I really have no idea what to do.

Thanks in advance for any help.

---EDIT---

In case this is helpful: this is the generated query without any filters active (just the default woocommerce query, which works without any problems):

{
"query": {
    "post_type": "product"
},
"query_vars": {
    "post_type": "product",
    "error": "",
    "m": "",
    "p": 0,
    "post_parent": "",
    "subpost": "",
    "subpost_id": "",
    "attachment": "",
    "attachment_id": 0,
    "name": "",
    "pagename": "",
    "page_id": 0,
    "second": "",
    "minute": "",
    "hour": "",
    "day": 0,
    "monthnum": 0,
    "year": 0,
    "w": 0,
    "category_name": "",
    "tag": "",
    "cat": "",
    "tag_id": "",
    "author": "",
    "author_name": "",
    "feed": "",
    "tb": "",
    "paged": 0,
    "meta_key": "",
    "meta_value": "",
    "preview": "",
    "s": "",
    "sentence": "",
    "title": "",
    "fields": "",
    "menu_order": "",
    "embed": "",
    "category__in": [],
    "category__not_in": [],
    "category__and": [],
    "post__in": [],
    "post__not_in": [],
    "post_name__in": [],
    "tag__in": [],
    "tag__not_in": [],
    "tag__and": [],
    "tag_slug__in": [],
    "tag_slug__and": [],
    "post_parent__in": [],
    "post_parent__not_in": [],
    "author__in": [],
    "author__not_in": [],
    "orderby": "popularity",
    "order": "ASC",
    "meta_query": [],
    "tax_query": {
    "relation": "AND"
    },
    "wc_query": "product_query",
    "posts_per_page": 48
},
"tax_query": {
    "queries": [],
    "relation": "AND",
    "queried_terms": [],
    "primary_table": null,
    "primary_id_column": null
},
"meta_query": false,
"date_query": false,
"post_count": 0,
"current_post": -1,
"in_the_loop": false,
"comment_count": 0,
"current_comment": -1,
"found_posts": 0,
"max_num_pages": 0,
"max_num_comment_pages": 0,
"is_single": false,
"is_preview": false,
"is_page": false,
"is_archive": true,
"is_date": false,
"is_year": false,
"is_month": false,
"is_day": false,
"is_time": false,
"is_author": false,
"is_category": false,
"is_tag": false,
"is_tax": false,
"is_search": false,
"is_feed": false,
"is_comment_feed": false,
"is_trackback": false,
"is_home": false,
"is_privacy_policy": false,
"is_404": false,
"is_embed": false,
"is_paged": false,
"is_admin": false,
"is_attachment": false,
"is_singular": false,
"is_robots": false,
"is_favicon": false,
"is_posts_page": false,
"is_post_type_archive": true,
"thumbnails_cached": false
}

And this is the generated query when I set the "autor" filter as "Arcane Minis", and the "orderby" to "popularity":

{
"query": {
    "orderby": "",
    "post_type": "product"
},
"query_vars": {
    "orderby": "popularity",
    "post_type": "product",
    "error": "",
    "m": "",
    "p": 0,
    "post_parent": "",
    "subpost": "",
    "subpost_id": "",
    "attachment": "",
    "attachment_id": 0,
    "name": "",
    "pagename": "",
    "page_id": 0,
    "second": "",
    "minute": "",
    "hour": "",
    "day": 0,
    "monthnum": 0,
    "year": 0,
    "w": 0,
    "category_name": "",
    "tag": "",
    "cat": "",
    "tag_id": "",
    "author": "",
    "author_name": "",
    "feed": "",
    "tb": "",
    "paged": 0,
    "meta_key": "",
    "meta_value": "",
    "preview": "",
    "s": "",
    "sentence": "",
    "title": "",
    "fields": "",
    "menu_order": "",
    "embed": "",
    "category__in": [],
    "category__not_in": [],
    "category__and": [],
    "post__in": [],
    "post__not_in": [],
    "post_name__in": [],
    "tag__in": [],
    "tag__not_in": [],
    "tag__and": [],
    "tag_slug__in": [],
    "tag_slug__and": [],
    "post_parent__in": [],
    "post_parent__not_in": [],
    "author__in": [],
    "author__not_in": [],
    "order": "ASC",
    "meta_query": [],
    "tax_query": {
    "0": {
        "taxonomy": "autor",
        "field": "slug",
        "terms": [
        "arcane-minis"
        ],
        "operator": "IN"
    },
    "relation": "AND"
    },
    "wc_query": "product_query",
    "posts_per_page": 48
},
"tax_query": {
    "queries": [],
    "relation": "AND",
    "queried_terms": [],
    "primary_table": null,
    "primary_id_column": null
},
"meta_query": false,
"date_query": false,
"post_count": 0,
"current_post": -1,
"in_the_loop": false,
"comment_count": 0,
"current_comment": -1,
"found_posts": 0,
"max_num_pages": 0,
"max_num_comment_pages": 0,
"is_single": false,
"is_preview": false,
"is_page": false,
"is_archive": true,
"is_date": false,
"is_year": false,
"is_month": false,
"is_day": false,
"is_time": false,
"is_author": false,
"is_category": false,
"is_tag": false,
"is_tax": false,
"is_search": false,
"is_feed": false,
"is_comment_feed": false,
"is_trackback": false,
"is_home": false,
"is_privacy_policy": false,
"is_404": false,
"is_embed": false,
"is_paged": false,
"is_admin": false,
"is_attachment": false,
"is_singular": false,
"is_robots": false,
"is_favicon": false,
"is_posts_page": false,
"is_post_type_archive": true,
"thumbnails_cached": false
}

And lastly, this is the filtered query without elastic press:

{
"query": {
    "orderby": "",
    "post_type": "product"
},
"query_vars": {
    "orderby": "popularity",
    "post_type": "product",
    "error": "",
    "m": "",
    "p": 0,
    "post_parent": "",
    "subpost": "",
    "subpost_id": "",
    "attachment": "",
    "attachment_id": 0,
    "name": "",
    "pagename": "",
    "page_id": 0,
    "second": "",
    "minute": "",
    "hour": "",
    "day": 0,
    "monthnum": 0,
    "year": 0,
    "w": 0,
    "category_name": "",
    "tag": "",
    "cat": "",
    "tag_id": "",
    "author": "",
    "author_name": "",
    "feed": "",
    "tb": "",
    "paged": 0,
    "meta_key": "",
    "meta_value": "",
    "preview": "",
    "s": "",
    "sentence": "",
    "title": "",
    "fields": "",
    "menu_order": "",
    "embed": "",
    "category__in": [],
    "category__not_in": [],
    "category__and": [],
    "post__in": [],
    "post__not_in": [],
    "post_name__in": [],
    "tag__in": [],
    "tag__not_in": [],
    "tag__and": [],
    "tag_slug__in": [],
    "tag_slug__and": [],
    "post_parent__in": [],
    "post_parent__not_in": [],
    "author__in": [],
    "author__not_in": [],
    "order": "ASC",
    "meta_query": [],
    "tax_query": {
    "0": {
        "taxonomy": "autor",
        "field": "slug",
        "terms": [
        "arcane-minis"
        ],
        "operator": "IN"
    },
    "relation": "AND"
    },
    "wc_query": "product_query",
    "posts_per_page": 48
},
"tax_query": {
    "queries": [],
    "relation": "AND",
    "queried_terms": [],
    "primary_table": null,
    "primary_id_column": null
},
"meta_query": false,
"date_query": false,
"post_count": 0,
"current_post": -1,
"in_the_loop": false,
"comment_count": 0,
"current_comment": -1,
"found_posts": 0,
"max_num_pages": 0,
"max_num_comment_pages": 0,
"is_single": false,
"is_preview": false,
"is_page": false,
"is_archive": true,
"is_date": false,
"is_year": false,
"is_month": false,
"is_day": false,
"is_time": false,
"is_author": false,
"is_category": false,
"is_tag": false,
"is_tax": false,
"is_search": false,
"is_feed": false,
"is_comment_feed": false,
"is_trackback": false,
"is_home": false,
"is_privacy_policy": false,
"is_404": false,
"is_embed": false,
"is_paged": false,
"is_admin": false,
"is_attachment": false,
"is_singular": false,
"is_robots": false,
"is_favicon": false,
"is_posts_page": false,
"is_post_type_archive": true,
"thumbnails_cached": false
}
5
  • I know EP allows you to explicitly define that it should be used in a query, check if they have a similar parameter that allows you to explicitly tell it to NOT become involved. Jan 28, 2021 at 17:08
  • @TonyDjukic That's pretty interesting, thanks! It's definetly not the ideal solution, since I want elastic's capabilities on these queries, but if I can't find any other solution, that would probably be a nice compromise. Jan 28, 2021 at 17:12
  • :facepalm: Didn't even occur to me that you WANT ElasticPress still involved. Sorry. Jan 28, 2021 at 17:14
  • @TonyDjukic As I said, that's a nice compromise, in case I can't find another solution. I tried adding $q->set('ep_integrate', false); to my query, but when I check it with the query monitor, it says ep_integrate is set to 1. It seems elasticpress is happening after my query. Is there anyway I can increase my query's priority, like an !important in CSS? Jan 28, 2021 at 17:33
  • Acutally, I'm just kinda dumb. I had the $q->set('ep_integrate', false); under the wrong conditional. So yeah, in case I can't solve this problem, I'll just turn off EP on this query. Jan 28, 2021 at 17:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy