我想把最后32个上传的产品放在一个特定的类别中。
该函数可以工作,但在上传新产品时,它不会从旧产品中删除该类别。
以下是代码:
function add_category_to_product()
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 32
);
$products = new WP_Query($args);
$foundposts = $products->post_count;
if ($products->have_posts()) {
while ($products->have_posts()) {
$products->the_post();
if ($foundposts <= 32 && has_term(array(86, 87, 88, 89, 90, 91, 92, 93, 83, 94, 95, 96, 84, 98, 99, 100, 101, 102, 103, 80), 'product_cat', get_the_ID())) {
wp_set_object_terms(get_the_ID(), 79, 'product_cat', true);
} elseif ($foundposts <= 32 && has_term(array(51, 52, 53, 54, 55, 56, 57, 58, 43, 60, 61, 62, 63, 45, 64, 65, 66, 67, 68, 69, 70, 48, 71, 72, 73, 74, 75, 76, 77, 78, 44, 49), 'product_cat', get_the_ID())) {
wp_set_object_terms(get_the_ID(), 40, 'product_cat', true);
} elseif ($foundposts <= 32 && has_term(array(128, 129, 130, 131, 132, 114, 115, 133, 134, 135, 136, 117, 118, 119, 137, 138, 139, 140), 'product_cat', get_the_ID())) {
wp_set_object_terms(get_the_ID(), 112, 'product_cat', true);
} elseif ($foundposts <= 32 && has_term(array(141, 142, 143, 144, 145, 146, 125, 147, 148, 149, 150), 'product_cat', get_the_ID())) {
wp_set_object_terms(get_the_ID(), 122, 'product_cat', true);
} else {
wp_remove_object_terms(get_the_ID(), 112, 'product_cat');
wp_remove_object_terms(get_the_ID(), 122, 'product_cat');
wp_remove_object_terms(get_the_ID(), 40, 'product_cat');
wp_remove_object_terms(get_the_ID(), 79, 'product_cat');
}
}
wp_reset_postdata();
}
}也许有人能看到我的错误?
发布于 2021-11-10 14:45:59
您的错误:
您可以使用'posts_per_page' => 32并提到‘和其他产品’,而其他条件在这32个当前产品的循环中.
解决方案:
limit,它接受一个整数:要检索的结果的最大数量,或无限的-1。orderby -接受一个字符串:'none','ID','name','type','rand','date','modified‘。order -接受一个字符串:'DESC‘或'ASC’。与orderby一起使用。return -接受一个字符串:ids或objects.,因为我们只需要针对产品对象的productID,ids就足够了。使用的WordPress函数:
所以你得到了:
// For x number of last products
$last_products = 32;
// Args
$args = array(
'limit' => -1,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
);
// Get product IDs
$product_ids = wc_get_products( $args );
// Loop through
foreach( $product_ids as $key => $product_id ) {
if ( ( $key + 1 ) <= $last_products ) {
if ( has_term( array( 80, 86, 87, 88, 89, 90, 91, 92, 93, 83, 94, 95, 96, 84, 98, 99, 100, 101, 102, 103 ), 'product_cat', $product_id ) ) {
wp_set_object_terms( $product_id, 79, 'product_cat', true );
} elseif ( has_term( array( 44, 49, 51, 52, 53, 54, 55, 56, 57, 58, 43, 60, 61, 62, 63, 45, 64, 65, 66, 67, 68, 69, 70, 48, 71, 72, 73, 74, 75, 76, 77, 78 ), 'product_cat', $product_id ) ) {
wp_set_object_terms( $product_id, 40, 'product_cat', true );
} // etc..
} else {
if ( has_term( array ( 40, 79, 112, 122 ), 'product_cat', $product_id ) ) {
wp_remove_object_terms( $product_id, 40, 'product_cat', true );
wp_remove_object_terms( $product_id, 79, 'product_cat', true );
wp_remove_object_terms( $product_id, 112, 'product_cat', true );
wp_remove_object_terms( $product_id, 122, 'product_cat', true );
}
}
}https://stackoverflow.com/questions/69912627
复制相似问题