I want to set the last 32 uploaded products in a certain category.
The function works but it doesn't remove the category from the older products, when new products are uploaded.
Here is the code:
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();
}
}
Maybe someone can see my mistake?
CodePudding user response:
Your mistake:
You use 'posts_per_page' => 32
and mention 'and for the other products', while the else condition is in the loop of those 32 current products...
The solution:
Since you actually want to apply a change for all products (unless you perform this function on a regular basis), just applying to the last 32 products will not suffice. So use
limit
, which accepts an integer: Maximum number of results to retrieve or-1
for unlimited.orderby
- Accepts a string: 'none', 'ID', 'name', 'type', 'rand', 'date', 'modified'.order
- Accepts a string: 'DESC' or 'ASC'. Use withorderby
.return
- Accepts a string:ids
orobjects.
Since we only need the productID against the product object, willids
suffice.Note: This way of making changes to products is effective when there are not a lot of products involved. If not, then a sql query is a much more efficient solution.
WordPress functions used:
So you get:
// 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 );
}
}
}