0

需要更新自定义字段值,如果它是空白错误如果它有任何值不想做任何事情

我只想在它是空代码时更新:

$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);
    }
4

1 回答 1

0

下面的示例可能会有所帮助:

`

        

    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);
    }
    ?>

`

将 get_postmeta_details_in_array() 和 update_postmeta_by_meta_id() 函数放在 wordpress 主题的 functions.php 中。

于 2012-04-15T13:32:16.753 回答