WordPress.org

Ready to get started?Download WordPress

Forums

Removing comma's in a custom field from the database (6 posts)

  1. Wnd
    Member
    Posted 1 year ago #

    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,

  2. Wnd
    Member
    Posted 1 year ago #

    Or is it possible to ignore the commma while sorting?

  3. dotnordic
    Member
    Posted 1 year ago #

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

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

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

  4. Wnd
    Member
    Posted 1 year ago #

    Many many thanks! I will test it soon (;

  5. Wnd
    Member
    Posted 1 year ago #

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

  6. Wnd
    Member
    Posted 1 year ago #

    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';
    }

Topic Closed

This topic has been closed to new replies.

About this Topic