wpsolr
Keymaster
6 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 );
}