我想在某些产品类别中为woocommerce产品设置属性。但是,该查询似乎不起作用。
我已将以下代码放入子主题的functions.php中。 尝试用term_id替换段塞,尝试添加“关系”,以防万一,尝试显式设置段塞而不是变量,但还是没有运气。 UPD:wc_get_products和WC_Product_Query也不起作用。
function wccategory_set_attributes($categoryslug, $attributes) {
$pquery = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $categoryslug,
),
),
));
if( $pquery->have_posts() ) {
while ($pquery->have_posts()) : $pquery->the_post();
foreach ($attributes as $name => $value) {
wp_set_object_terms( $postid, $value, $name, false );
$product_attributes[$i] = array (
'name' => htmlspecialchars( stripslashes( $name ) ),
'value' => $value,
'is_visible' => 1,
'is_taxonomy' => 0
);
$i++;
}
update_post_meta( $postid,'_product_attributes', $product_attributes);
endwhile;
}
}
wccategory_set_attributes('theslug', array ( 'pa_length' => '', 'pa_width' => '', 'pa_type' => ''));
$ pquery-> have_posts()不返回任何内容。这些帖子仍然存在。一旦我从参数中删除了“ tax_query”,一切都将起作用。我假设“ tax_query”中存在一些错误。我看了很多其他示例,看起来不错,但似乎缺少了一些东西。
答案 0 :(得分:0)
您是否尝试过使用get_posts()?如果您只是尝试检索帖子计数,则可能不需要完整查询,并且get_posts会自行返回一个数组。
此外,如果您要返回的唯一内容是发件计数,而不是尝试使用函数外部的返回项目,仅返回计数?最后,post_count与found_posts不同。如果每页设置了一个帖子,则您的post_count将始终以该数量最大化,found_posts将提供在该查询中找到的所有帖子。
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"message": "*GET*/event-heartbeat*"
}
}
],
"minimum_should_match": 1
}
}
}