• I have a problem with my price range. It doesn’t order the way i want, because 1,200.00 comes before 120.00.

    Is there a way (maybe a php function) to remove comma’s in custom fields at a post? I just want to remove all commas in the custom field “price” from the database.

    I already searched the internet for 4 hours but can’t find a solution.

    Many thanks,

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Wnd

    (@wnd)

    Or is it possible to ignore the commma while sorting?

    Remove both comma and period before you insert the values into the database.

    str_replace(array('.', ','), '' , $string)

    https://php.net/manual/en/function.str-replace.php

    Thread Starter Wnd

    (@wnd)

    Many many thanks! I will test it soon (;

    Thread Starter Wnd

    (@wnd)

    I still have a question about the str_replace. How can i use it in a function?

    Thread Starter Wnd

    (@wnd)

    Allright,.. the code works. It removes the comma so thats great! But how can i change the old price value to the new price value in the database?

    I tried:

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

    and

    add_post_meta($postID, $count_key, $newPrice);

    But both code lines didn’t work. What am i doing wrong?

    Here is my function to delete the comma and return the new price:

    function deleteComma($postID){
        $price_key = 'price';
        $thePrice = get_post_meta($postID, $price_key, true);
        $newPrice = str_replace(array(','), '' , $thePrice);
    
        return $newPrice.'Dollar';
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Removing comma's in a custom field from the database’ is closed to new replies.