2

I have a meta key called 'prijs' in each post. The value of this meta key consist of numbers in the follow structure: 2,100.00

I want to remove the comma in the meta value to get this output: 2100.00

So, i created a function but it doesn't work:

add_action('wp_insert_post', 'deleteCommaDB');
function deleteCommaDB($postID){
    $price_key = 'prijs';
    $getPrice = get_post_meta($postID, $price_key, true);
    $newPrice = str_replace(array(','), '', $getPrice);

    update_post_meta($postID, $price_key, $newPrice);

    return true;
}

I created the function deleteCommaDB. This function gets the post meta from the meta key 'prijs', and replaces the comma for nothing. This new value is saved in the variable $newPrice. Next thing to do, is update the post meta with the new value.

What am i doing wrong?

4
  • Where does the problem occur? What part fails?
    – s_ha_dum
    Feb 27, 2013 at 16:49
  • What you are doing will just replace the new format for newly inserted posts not for the old ones. Do you want to change to old ones? Feb 27, 2013 at 17:00
  • @s_ha_dum Well, the $newPrice value output is correct, but it doesnt change the value in the database
    – ElBrm
    Feb 27, 2013 at 20:38
  • @Sunyatasattva Even if i create new posts, it doesn't work.
    – ElBrm
    Feb 27, 2013 at 20:39

3 Answers 3

1

You'll only need to run it once, but this should get all your posts, and then loop through them and update the meta.

add_action('admin_init', 'deleteCommaDB');

function deleteCommaDB(){

    // The Query
    $args = array ( 'posts_per_page' => -1 );
    $the_query = new WP_Query( $args );

    // The Loop
    while ( $the_query->have_posts() ) :
        $the_query->the_post();

         $price_key = 'prijs';
        $getPrice = get_post_meta( get_the_ID(), $price_key, true);
        $newPrice = str_replace(array(','), '', $getPrice);
        update_post_meta(get_the_ID(), $price_key, $newPrice);

    endwhile;

}

Your function runs only when a post is inserted. You might want to consider running on save_post and sanitizing the $_POST input in the first place.

7
  • The only change you made is running the function in a loop right? My function doesn't run trough all the posts, but runs only on the first post? The function above doesn't work for me.
    – ElBrm
    Feb 27, 2013 at 20:49
  • The function in your question only runs on the wp_insert_post hook which only runs the first time a post is created. My answer should query all the posts and then loop through them. What part is not working? Are you working with a custom post type? That's the only thing I can think would prevent this code from working. Feb 27, 2013 at 20:54
  • I'm using the plugin WP AllImport to create my posts automatic. BUT, your function works when i change 'admin_init' to 'wp_insert_post'! I have to update the posts again ofcourse. If i use admin_init, it doesn't work. But i'm satisfied with how it works right now (; Is it possible that admin_init doesn't work because of the plugin? By the way: THANKS!
    – ElBrm
    Feb 27, 2013 at 21:14
  • admin_init runs any time you load a page in the admin. So just refresh the admin anywhere. Feb 27, 2013 at 21:28
  • 1
    PS- This was under the assumption that you already HAD post meta that you wanted to convert. If you are trying to sanitize data as new posts are created then you'd do something different... as I mentioned you'd run an action on save_post. Feb 27, 2013 at 21:31
1

add_action('wp_insert_post', 'deleteCommaDB'); - this line calls your function only on post creation.

In order to change all previously saved posts, you will need to add your function in a wordpress loop that gets all the posts.

1

Always take the right tool for the right job

No need for str_replace() and other complicated or slow things. Use what PHP or WordPress deliver for such cases:

For the record: There's also a function called money_format().

4
  • Doesn't number_format() format a float variable? Meaning if you try to use it on a number such as 2,000 the floated value will be 2 and so it will format incorrectly? Feb 28, 2013 at 13:25
  • @helgatheviking You can check that up front and modify the number of decimals.
    – kaiser
    Feb 28, 2013 at 13:36
  • Thanks @kaiser. But if you do that aren't you still going to need to str_replace? Feb 28, 2013 at 16:38
  • @helgatheviking No, as you simply switch the second argument with if/else.
    – kaiser
    Mar 1, 2013 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.