0

When a use it like this:

add_post_meta($post_ID, 'Name', "Bob", true );   

works fine, but when a store an string value inside a variable, for example:

$name = "Bob";
add_post_meta($post_ID, 'Name', $name, true ); // <---- This doesn't work.

Some help please. Thanks.

Ruvee
  • 8,611
  • 4
  • 18
  • 44
ecv86
  • 1

2 Answers2

0

Try the below code.

$name = "Bob"; 
update_post_meta($post_ID, 'Name', $name );
Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • Thanks for the answer, but the problem still there. Let me explain better. I have a custom post with 3 custom fields, this fields are generated when the user save it as draft, the problems is that i need to fill them with values from other custom post, in this case the most recent one. In this case i can get the value without any problem, but when i use the add_post_meta function to insert the values through variables the fields appears empty, however when change the variables for simples strings like "Bob" or "Jake" its work fine. – ecv86 Apr 01 '21 at 18:42
0
$latestVideo = wp_get_recent_posts(array(
    'numberposts' => 1,
    'post_type'   =>'video',
    'post_status' => 'publish'
));

$latestVideoWeek = get_post_meta( $latestVideo[0]["ID"], "Week", true);
$latestVideoDate = get_post_meta( $latestVideo[0]["ID"], "Date", true);

function meta_info_video( $post_ID ) {
    add_post_meta( $post_ID, 'Semana', $latestVideoWeek, true );
    add_post_meta( $post_ID, 'Fecha', $latestVideoDate, true );
    add_post_meta( $post_ID, 'URL', '0', true  );
}

add_action( 'draft_video', 'meta_info_video' );
ecv86
  • 1