function test_function($query) {
if (!$query->is_main_query() || is_admin()) {
return;
}
if ($outofstock_term = get_term_by('name', 'outofstock', 'product_visibility')) {
$tax_query = (array) $query->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array($outofstock_term->term_taxonomy_id),
'operator' => 'NOT IN'
);
$query->set('tax_query', $tax_query);
}
$meta_query = array(
array(
'key' => 'test_visibility',
'value' => 'vis',
'operator' => 'NOT LIKE'
)
);
$query->set('meta_query', $meta_query);
}
add_action('pre_get_posts', 'test_function');
$args = [
'post_type' => 'post',
'_meta_or_tax' => true, // <-- our new custom parameter
'tax_query' => [...], // <-- our tax query
'meta_query' => [...], // <-- our meta query
];
$query = new WP_Query( $args );
/**
* Modify WP_Query to support 'meta_or_tax' argument
* to use OR between meta- and taxonomy query parts.
*/
add_filter( 'posts_where', function( $where, \WP_Query $q )
{
// Get query vars
$tax_args = isset( $q->query_vars['tax_query'] )
? $q->query_vars['tax_query']
: null;
$meta_args = isset( $q->query_vars['meta_query'] )
? $q->query_vars['meta_query']
: null;
$meta_or_tax = isset( $q->query_vars['_meta_or_tax'] )
? wp_validate_boolean( $q->query_vars['_meta_or_tax'] )
: false;
// Construct the "tax OR meta" query
if( $meta_or_tax && is_array( $tax_args ) && is_array( $meta_args ) )
{
global $wpdb;
// Primary id column
$field = 'ID';
// Tax query
$sql_tax = get_tax_sql( $tax_args, $wpdb->posts, $field );
// Meta query
$sql_meta = get_meta_sql( $meta_args, 'post', $wpdb->posts, $field );
// Modify the 'where' part
if( isset( $sql_meta['where'] ) && isset( $sql_tax['where'] ) )
{
$where = str_replace(
[ $sql_meta['where'], $sql_tax['where'] ],
'',
$where
);
$where .= sprintf(
' AND ( %s OR %s ) ',
substr( trim( $sql_meta['where'] ), 4 ),
substr( trim( $sql_tax['where'] ), 4 )
);
}
}
return $where;
}, PHP_INT_MAX, 2 );