如何进行查询以便显示所有产品,但缺货除外,但如果元字段是“可见的”,则显示商品和缺货商品?
由于
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' => 'hidden',
'operator' => 'NOT LIKE'
)
);
$query->set('meta_query', $meta_query);
}
add_action('pre_get_posts', 'test_function');
答案 0 :(得分:0)
如何在WP查询中添加元查询和税务查询?
<?php
$cats_args=array(
'type' => 'products', //posttype name
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => -1,
'meta_query' => array(
array('key' => 'test_visibility',
'value' => 'hidden',
'compare' => '!=',
)
),
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => true,
'exclude' => '',
'include' => '',
'number' => '',
'tax_query' => array(
array(
'taxonomy' => 'product_visibility', //taxonomy name
'field' => 'name',
'terms' => 'outofstock',
'operator' => 'NOT IN'
)
),
);
$cats_my_query = null;
$cats_my_query = new WP_Query($cats_args);
if( $cats_my_query->have_posts() )
{
$cats_count=0;
while ($cats_my_query->have_posts()) : $cats_my_query->the_post();
$cats_count++;
echo $cats_my_query->post_name;
endwhile;
}
wp_reset_query($cats_my_query);
?>