最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - How to save a meta_value as a numeric value after I retrieve it via update_post_meta?

programmeradmin0浏览0评论

I tried things like $meta_rwp_user_score = (number_format_i18n($meta_rwp_user_score, 2)); , but this is not working. Where do I use that?

<?php
$meta_rwp_user_score['meta_rwp_user_score'] = get_post_meta(($anbieter_id),'rwp_user_score',true);

foreach ( $meta_rwp_user_score as $key => $value ) :
  if ( 'revision' === $post->post_type ) {
    return;
  }

  if ( get_post_meta( $postid, $key, false ) ) {
    // If the custom field already has a value, update it.
    update_post_meta( $postid, $key, $value );
  } else {
    // If the custom field doesn't have a value, add it.
    add_post_meta( $postid, $key, $value);
  }

  if ( ! $value ) {
    // Delete the meta key if there's no value
    delete_post_meta( $postid, $key );
  }

endforeach;
?>

I tried things like $meta_rwp_user_score = (number_format_i18n($meta_rwp_user_score, 2)); , but this is not working. Where do I use that?

<?php
$meta_rwp_user_score['meta_rwp_user_score'] = get_post_meta(($anbieter_id),'rwp_user_score',true);

foreach ( $meta_rwp_user_score as $key => $value ) :
  if ( 'revision' === $post->post_type ) {
    return;
  }

  if ( get_post_meta( $postid, $key, false ) ) {
    // If the custom field already has a value, update it.
    update_post_meta( $postid, $key, $value );
  } else {
    // If the custom field doesn't have a value, add it.
    add_post_meta( $postid, $key, $value);
  }

  if ( ! $value ) {
    // Delete the meta key if there's no value
    delete_post_meta( $postid, $key );
  }

endforeach;
?>
Share Improve this question asked May 21, 2020 at 12:45 meicelinhomeicelinho 32 bronze badges 3
  • If you go to database using such as phpmyadmin, you will find that the post_meta meta_value is set as longtext. I believe it is because meta_value is served as a generic purpose for all situation. You will need to handle it by case when retrieving it. – 西門 正 Code Guy - JingCodeGuy Commented May 21, 2020 at 22:27
  • Yeah, that's right. What would you prefer to do now? I'm still on a beginner level of programming. – meicelinho Commented May 21, 2020 at 23:07
  • I have added an example on converting it to float number before you use it. So it might help. See if it is what you are looking for. Sorry that last week was swamped. – 西門 正 Code Guy - JingCodeGuy Commented May 24, 2020 at 15:21
Add a comment  | 

1 Answer 1

Reset to default 0

According to the database schema, the meta_value is stored as longtext. It is probably because of generic purpose. So, to obtain a numeric value. There are 2 parts for doing so.

Saving

Although it is storing as longtext, it is still good for preparing the rightful numeric format so that when retrieve it, it is likely to be expected value for conversion. So, for checking before saving, may use is_float() or is_numeric() and so on to make sure the storing value is expected.

Retrieving

If the type float number is required to manipulate it correctly in the code after fetching the meta value from database. You may consider using php built-in function floatval() for float number to convert it before using. Another one is intval() for integer.

$meta_rwp_user_score['meta_rwp_user_score'] = floatval( get_post_meta(($anbieter_id),'rwp_user_score',true) );

// any text will be converted to 0

// manipulate as needed
发布评论

评论列表(0)

  1. 暂无评论