我正在用這個更新帖子元
update_post_meta( '3100', 'mymetakey', 'mymetavalue' );
我怎樣才能添加這個元鍵和元值,不僅要發布 ID 3100,還要添加其他幾個 ID?
uj5u.com熱心網友回復:
您可以定義array帖子 ID,然后您可以迭代 ID 回圈。試試下面的代碼。
$postIds = array( 3100, 1234, 5678....);
foreach ( $postIds as $key => $post_id ) {
update_post_meta( $post_id, 'mymetakey', 'mymetavalue' );
}
根據 OP 評論更新。
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat', // custom taxonomy
'field' => 'slug',
'terms' => 'your-category-slug', // taxonomy term (category)
)
)
);
$products = new WP_Query( $args );
if( $products->have_have() ){ while ( $products->have_have() ) { $products->the_post();
update_post_meta( get_the_ID(), 'mymetakey', 'mymetavalue' );
} wp_reset_postdata(); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/366677.html
標籤:php WordPress的 元
上一篇:在保存訂單和發送WooCommerce電子郵件通知之前,將訂單ID添加到訂單項元資料
下一篇:在回圈內使用ACF更改類別名稱
