Filter the search results by taxonomy

  • pseudo visitor
    Participant
    5 years, 4 months ago #8640

    Hi there.

    I have a question: I would like to filter the search results by taxonomy. Is there a way to do that? I’m not having any luck on your documentation…

    wpsolr
    Keymaster
    5 years, 4 months ago #8641

    Hi,

    Will you pass the taxonomy in the url parameters ?

    pseudo visitor
    Participant
    5 years, 4 months ago #8642

    Exactly ????

    I was using the “pre_get_posts” hook before to filter the search results, but since using WPSOLR, it isn’t working anymore.

    Here you have an example of what the URL will look like: https://www.testwebsite.com/?s=tste&tax=com-usar

    wpsolr
    Keymaster
    5 years, 4 months ago #8643

    Then it’s simple.

    To know which parameters to use, just configure your taxonomy as a facet in screen 2.4.

    The taxonomy then appears in the facets widget. Click on a taxonomy, and observe the url. This is your parameter.

    pseudo visitor
    Participant
    5 years, 4 months ago #8644

    By doing that, it only allows me to filter by a term inside this taxonomy. Is there a way to show the posts and terms only for the desired taxonomy?

    For example: by passing “?tax=tax1” it only shows the terms and posts for that taxonomy.

    wpsolr
    Keymaster
    5 years, 4 months ago #8645

    You mean to filter out posts with no term in the taxonomy?

    wpsolr
    Keymaster
    5 years, 4 months ago #8646

    To display only posts with no value in a taxonomy field:

    add_action( WPSOLR_Events::WPSOLR_ACTION_SOLARIUM_QUERY, [
     $this,
     'wpsolr_action_query',
    ], 10, 1 );
    
    /**
     *
     * Add a "not exists" filter on a taxonomy.
     *
     * @param array $parameters
     *
     */
    public function wpsolr_action_query( $parameters ) {
    
    $taxonomy_name = 'my-taxonomy';
    
     /* @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 taxonomy {$taxonomy_name}", $filter_not_exists_terms_in_taxonomy );
    }
    pseudo visitor
    Participant
    5 years, 4 months ago #8647

    Exactly, that’s it ????

    Where should I insert this code? When I do it in my theme’s functions.php, I get the following error:
    Fatal error: Uncaught Error: Using $this when not in object context in …
    Where does “$this” come from?

    wpsolr
    Keymaster
    5 years, 4 months ago #8648

    Because you’re in a function, not in a class.

    Use instead:
    add_action( WPSOLR_Events::WPSOLR_ACTION_SOLARIUM_QUERY, 'wpsolr_action_query', 10, 1 );

    wpsolr
    Keymaster
    5 years, 4 months ago #8649

    Also check some more details in case of troubles at https://www.wpsolr.com/guide-category/actions-and-filters/

    pseudo visitor
    Participant
    5 years, 4 months ago #8650

    I now have this issue:
    Fatal error: Uncaught Error: Call to a member function search_engine_client_create_not() on null

    on the following line:

    $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));

    wpsolr
    Keymaster
    5 years, 4 months ago #8651

    Can I see your code?

    pseudo visitor
    Participant
    5 years, 4 months ago #8652

    Of course.

    Inside my functions.php I’m doing the following:

    use wpsolr\core\classes\WPSOLR_Events;
    
    use Solarium\QueryType\Select\Query\FilterQuery;
    
    use Solarium\QueryType\Select\Query\Query;
    
    use wpsolr\core\classes\engines\solarium\admin\WPSOLR_Solr_Admin_Api_Core;
    
    use wpsolr\core\classes\engines\WPSOLR_AbstractSearchClient;
    
    use wpsolr\core\classes\hosting_api\WPSOLR_Hosting_Api_Abstract;
    
    use wpsolr\core\classes\services\WPSOLR_Service_Container;
    
    use wpsolr\core\classes\utilities\WPSOLR_Regexp;
    
    use wpsolr\core\classes\WpSolrSchema;
    
    add_action(WPSOLR_Events::WPSOLR_ACTION_SOLARIUM_QUERY, 'wpsolr_action_query', 10, 1);
    
    function wpsolr_action_query($parameters)
    
    {
    
    $taxonomy_name = 'tax1';
    
    /* @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 taxonomy {$taxonomy_name}", $filter_not_exists_terms_in_taxonomy);
    
    }
    wpsolr
    Keymaster
    5 years, 4 months ago #8653
    pseudo visitor
    Participant
    5 years, 4 months ago #8654

    Thank you, I was able to make it work finally ????
    I also figured out that to have the same effect on several taxonomies I just have to repeat the process for each one ????

    Again, thank you for everything and for your patience.

Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic.