Question

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;
?>
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top