Filter the search results by taxonomy
-
pseudo visitorParticipant4 years, 6 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…
pseudo visitorParticipant4 years, 6 months ago #8642Exactly ????
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
pseudo visitorParticipant4 years, 6 months ago #8644By 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.
wpsolrKeymaster4 years, 6 months ago #8646To 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 visitorParticipant4 years, 6 months ago #8647Exactly, 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?wpsolrKeymaster4 years, 6 months ago #8649Also check some more details in case of troubles at https://www.wpsolr.com/guide-category/actions-and-filters/
pseudo visitorParticipant4 years, 6 months ago #8650I 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));
pseudo visitorParticipant4 years, 6 months ago #8652Of 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); }
wpsolrKeymaster4 years, 6 months ago #8653Have a look at the very end of : https://www.wpsolr.com/guide-category/actions-and-filters/
pseudo visitorParticipant4 years, 6 months ago #8654Thank 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.
You must be logged in to reply to this topic.