Exclude Custom Taxonomy Temrs from WPSolr query

  • cbarwin
    Participant
    2 months ago #38275

    I need to add a filter in result set based on logged-In user-meta which has comma separated custom taxonomy terms.

    This article https://www.wpsolr.com/forums/topic/filter-the-search-results-by-taxonomy/ is exactly what I need but other way around.

    I want to exclude posts belong to given list of Taxonomy terms. Please help me achieve this.

    wpsolr
    Keymaster
    2 months ago #38276

    The article you mention should be a good start for your problem indeed. Extract taxonomy terms from loggedin user metas, then add them as “not in” filter to current WPSOLR query.

    cbarwin
    Participant
    2 months ago #38278

    Thank you for quick response.
    I am not able to get the desired results. Here is my code, its actually the code from mentioned article. Here, I am trying to pass the terms ‘PRICE1,PRICE2,PRICE3’ under taxonomy ‘pricetax’.

    function wpsolr_action_query( $parameters ) {
    $taxonomy_name = ‘pricetax’;
    /* @var WPSOLR_AbstractSearchClient $search_engine_client */
    $search_engine_client = $parameters[ WPSOLR_Events::WPSOLR_ACTION_SOLARIUM_QUERY__PARAM_SOLARIUM_CLIENT ];
    /**
    * Create a “not exists” filter
    */

    $filter_not_exists_terms_in_taxonomy = $search_engine_client->search_engine_client_create_not($search_engine_client->search_engine_client_create_filter_exists($taxonomy_name . WpSolrSchema::_SOLR_DYNAMIC_TYPE_STRING));

    $search_engine_client->search_engine_client_add_filter(“not in PRICE1,PRICE2,PRICE3 {$taxonomy_name}”, $filter_not_exists_terms_in_taxonomy);
    }

    Is there any documentation for this to understand how these filters work.

    Thank You.

    wpsolr
    Keymaster
    2 months ago #38279

    There are not documentation, as those methods are internal and therefore not officially supported.

    You can use the method search_engine_client_add_filter_not_in_terms(), with an example in /wpsolr/pro/extensions/woocommerce/class-wpsolr-plugin-woocommerce.php or /wpsolr/core/classes/extensions/premium/class-wpsolr-option-premium.php

    cbarwin
    Participant
    2 months ago #38280

    Thank you for the examples.
    Code below is working for some products while it does not seems to be working for others. Please help me what I am doing wrong here.

    function wpsolr_action_query( $parameters ) {
    $taxonomy_name = ‘pricetax’;
    /* @var WPSOLR_AbstractSearchClient $search_engine_client */
    $search_engine_client = $parameters[ WPSOLR_Events::WPSOLR_ACTION_SOLARIUM_QUERY__PARAM_SOLARIUM_CLIENT ];
    $search_engine_client->search_engine_client_add_filter_not_in_terms( ‘post_pricetax_not_user_restrict’, “pricetax_str”, [ ‘PRICE1′,’PRICE2′,’PRICE3′,’PRICE4’ ] );
    }

    Thank You.

    wpsolr
    Keymaster
    2 months ago #38281

    Configure the Query Monitor add-on, it will output the query generated by your code.

    You can then try the query in the Solr/Elasticsearch/OpenSearch dashboard, and update it accordingly to your needs.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.