wpsolr
Keymaster
1 year ago
#30869
The following code in your functions.php should filter out your products without a price from indexing.
Delete the content of your index to remove current indexed products with no price, then reindex.
<?php
use wpsolr\core\classes\WPSOLR_Events;
/**
* Modify the SQL query that retrieves all post types to be indexed, to remove products with some condition on _price
*/
add_filter( WPSOLR_Events::WPSOLR_FILTER_SQL_QUERY_STATEMENT,
function ( $sql_statements, $parameters ) {
global $wpdb;
$model_type = $parameters['model_type'];
if ( 'product' === $model_type->get_type() ) {
/*
* Use a WHERE sub query to filter out posts with _price not > 0
* This is an exemple: fine tune your query as you like
*/
$sql_statements['WHERE'] .= " AND A.ID in (SELECT post_id from {$wpdb->prefix}postmeta WHERE meta_key = '_price' AND CAST(meta_value as SIGNED) > 0) ";
/*
* We could also use a JOIN
* $sql_statements['JOIN'] .= '...' ;
*/
}
return $sql_statements;
}
, 10, 2 );