【问题标题】:Updating meta value for specific ids更新特定 ID 的元值
【发布时间】:2022-01-04 08:30:22
【问题描述】:

我正在用这个更新帖子元

update_post_meta( '3100', 'mymetakey', 'mymetavalue' );

我怎样才能将此元键和元值不仅添加到帖子 ID 3100,而且添加到其他几个 ID?

【问题讨论】:

    标签: php wordpress meta


    【解决方案1】:

    您可以定义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(); }
    

    【讨论】:

    • 太棒了。还有一种方法可以检查帖子(在我的情况下为产品)是否属于特定产品类别,而不是使用 ID?那么如果产品属于产品类别 A,更新元数据?
    • 您的意思是从特定类别中检索产品吗?
    • 是的。如果产品属于产品类别 A,则该类别中的所有产品都应收到元数据。所有其他人都没有。
    • 嗯..那不幸的是不起作用。我有一个序列化的元值,所以我添加了 $data = maybe_unserialize('a:1:{s:19:"order_list";a:1:{i:0;s:1:"1";}}' ) ;并在更新后的元数据中使用它,但它不起作用......
    猜你喜欢
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多