我想在某個類別中設定最后 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();
}
}
也許有人可以看到我的錯誤?
uj5u.com熱心網友回復:
你的錯誤:
您使用'posts_per_page' => 32并提及“and for the other products”,而 else 條件在這 32 個當前產品的回圈中......
解決方案:
使用wc_get_products 或 WC_Product_Query
由于您實際上想對所有產品應用更改(除非您定期執行此功能),因此僅應用到最后 32 個產品是不夠的。所以使用
limit,它接受一個整數:要檢索的最大結果數或-1無限制。orderby- 接受字串:'none'、'ID'、'name'、'type'、'rand'、'date'、'modified'。order- 接受字串:“DESC”或“ASC”。與 一起使用orderby。return- 接受一個字串:idsorobjects.因為我們只需要針對產品物件的 productID,就ids足夠了。注意:當涉及的產品不是很多時,這種對產品進行更改的方式是有效的。如果沒有,那么 sql 查詢是一個更有效的解決方案。
使用的 WordPress 功能:
wp_set_object_terms()
wp_remove_object_terms()
has_term()
所以你得到:
// 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://www.uj5u.com/shujuku/355211.html
標籤:php WordPress的 求购 产品 分类术语
