0

need to update a custom field value if it is blank mis if it has any value not want to do anything

I want to update only if it is empty code:

$custom_fields = get_post_custom($post_ID);//Current post id
    $my_custom_field = $custom_fields['artist'];//key name
    foreach ( $my_custom_field as $key => $value );
    if(empty($value) or ($value == '')){
        $artist_v = get_post_custom_values('artist', $post_ID);
        update_post_meta($post_ID, 'artist', $parent_title, $artist_v);
    }else{
        //add_post_meta($post_ID, 'artist', $parent_title, true);
    }

1 Answers1

0

Below example might help:

`

        

    if(!empty($ARR_meta_data))
    {
      foreach($ARR_meta_data as $meta)
      {
    if(trim($meta['meta_value']) == "")
    {
          update_postmeta_by_meta_id($meta['meta_id'], "  YOUR CUSTOM VALUE   ");
    }
      } 
    }

    function get_postmeta_details_in_array($post_id, $meta_key)
    {
      global $wpdb;
      $sql = "SELECT * FROM ".$wpdb->prefix."postmeta 
          WHERE meta_key='{$meta_key}' AND post_id='{$post_id}'";

      return $wpdb->get_results($sql, ARRAY_A);
    }

    function update_postmeta_by_meta_id($meta_id, $meta_value)
    {
      global $wpdb;
      $sql = "UPDATE ".$wpdb->prefix."postmeta SET meta_value='$meta_value' 
          WHERE meta_id='$meta_id'";

      return $wpdb->query($sql);
    }
    ?>

`

Place get_postmeta_details_in_array() and update_postmeta_by_meta_id() function in the functions.php in your wordpress theme.

Fazle Elahee
  • 599
  • 3
  • 14