admin管理员组

文章数量:1122832

I need to copy the Value from one Meta to another Meta.

Meta_key1 --> "Custom value1" (This value is inserted by the ERP) Meta_key2 --> "Custom value1" (I want it to copy the value of Meta_key1)

  1. First, as you see in the image below. In my store there is a Custom Meta that collects the Ean Code of the Product. This code is sent by the ERP. META = EAN, KEY = BARCODE

  2. Second. What I want is that the meta indicated in the picture, collects the same value as the EAN meta above.

@

I need to copy the Value from one Meta to another Meta.

Meta_key1 --> "Custom value1" (This value is inserted by the ERP) Meta_key2 --> "Custom value1" (I want it to copy the value of Meta_key1)

  1. First, as you see in the image below. In my store there is a Custom Meta that collects the Ean Code of the Product. This code is sent by the ERP. META = EAN, KEY = BARCODE

  2. Second. What I want is that the meta indicated in the picture, collects the same value as the EAN meta above.

@

Share Improve this question edited Apr 23, 2024 at 14:11 Pat J 12.3k2 gold badges28 silver badges36 bronze badges asked Apr 22, 2024 at 13:03 Amelia JCAmelia JC 11 bronze badge 1
  • 1 your question is not clear, can you please provide more details. – msz Commented Apr 22, 2024 at 14:54
Add a comment  | 

1 Answer 1

Reset to default 0

If you just need get_post_meta( $post_id, 'ywbc_barcode_display_value_custom_field', $single ); to return the value that's stored in ean, you can filter the get_post_meta() call to return that value:

add_filter( 'get_post_metadata', 'wpse424858_filter_meta_value', 10, 4 );
/**
 * Filters the barcode display meta value.
 *
 * @param mixed  $value  The existing meta value.
 * @param int    $id     The post ID.
 * @param string $key    The meta key. We're looking for 'ywbc_barcode_display_value_custom_field'.
 * @param bool   $single Return a single value or an array?
 */
function wpse424858_filter_meta_value( $value, $id, $key, $single ) {
    if ( 'ywbc_barcode_display_value_custom_field' === $key ) {
        // Gets the value stored in the 'ean' meta.
        return get_post_meta( $id, 'ean', $single );
    }
    // Otherwise, returns what we were provided.
    return $value;
}

Note: This code is untested and is meant as a starting point rather than a finished product. Try it out on a test site before putting it into production.

Doing it this way means that there's not a duplication of data in your DB.

References

  • get_{$meta_type}_metadata filter (the {$meta_type} in this case is post)

本文标签: copy the Value from one Meta to another Meta