Как обновить post meta товаров определенной категории?
Написал код для изменения мета поля при добавлении нового товар только выбранных категорий.
Код выглядит так.
add_action( 'woocommerce_product_options_general_product_data', 'add_deal_custom_general_product_field' );
function add_deal_custom_general_product_field() {
$args = array(
'limit' => -1, // All products
'status' => 'publish', // Only published products
'category' => array( 'novqe-12', 'novqe-4', 'novqe-3', 'novqe', 'novqe-10', 'novqe-6', 'novqe-2', 'novqe-9', 'novqe-8', 'novqe-7', 'novqe-11', 'novqe-5', 'novqe-sim-flash' ),
'return' => 'ids',
);
$products = wc_get_products( $args );
foreach( $products as $product ){
// update_term_meta( $product,'_yfym_condition', 'disabled', );
update_post_meta( $product, '_yfym_condition', 'disabled', 'default' );
}
}
Как изменить товары которые уже опубликованы на сайт?
Надо получать товары из базы данных и изменять мета поле товара?
Через get_posts() получаешь посты (товары) и циклом проходишься обновляешь все мету на ту которая нужна.
$my_posts = get_posts( [ 'numberposts' => -1, 'post_type' => 'product', 'tax_query' => [ [ // https://wp-kama.ru/function/WP_Query#taxonomies ] ] ] ); foreach( $my_posts as $pst ){ update_post_meta( $pst->ID, '_yfym_condition', 'disabled' ); }Спасибо. Все получилось!