• Resolved Begin

    (@bentalgad)


    I have something like 400 posts witch all have a custom field named “price”
    and in that custom field there is a different price for each post.

    The thing is it was written in that format: 20 dollars,
    and I need to change that to only contain the number without the word “dollars”.

    Is there a way to do that for all those posts at once, without needing
    to edit each one of those posts custom field?

    Thank’s!

Viewing 15 replies - 1 through 15 (of 16 total)
  • Hi Begin,

    You could do this through the backend in the database itself. Just identify the table and the of the custom field in that table and run a SQL statement that would select and remove dollars from that field. Make sure you make a backup of the database before running any SQL commands on the database. Your statement would look something like this (fill in your appropriate Table Name and Field Name):

    UPDATE TableName
    SET CustomFieldName = replace(CustomFieldName, ‘dollars’, ”);

    Hope this helps!

    Moderator keesiemeijer

    (@keesiemeijer)

    or try it with this in a page template file and load a page once.

    <?php
    $the_posts = get_posts('meta_key=price');
    foreach ( $the_posts as $the_post  ) {
    $price = trim(get_post_meta( $the_post->ID , 'price', true));
    $price = intval(trim(preg_replace('/ dollars/','', $price)));
    update_post_meta($the_post->ID , 'price', $price);
    }
    ?>

    as ChristiNi said make a backup of your database first.

    Thread Starter Begin

    (@bentalgad)

    Thank’s ChristiNi !

    I did used keesiemeijer method because it seemed easier…
    Thank’s keesiemeijer, your my hero today ??

    But for some reason it only change the 4-5 first posts and not all the rest… ?!

    Moderator keesiemeijer

    (@keesiemeijer)

    Are you sure they are formatted the same: “20 dollars”, “20 dollar”, “20dollars”

    Try changing this $the_posts = get_posts('meta_key=price'); to $the_posts = get_posts('meta_key=price&numberposts=-1');

    Thread Starter Begin

    (@bentalgad)

    Yes! that made it work for all of them!

    Thank you very very much keesiemeijer !!!
    You really saved me a lot of time today! Thank’s!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. Remove the lines from your page template.

    Thread Starter Begin

    (@bentalgad)

    Another question in the same subject…

    I have another custom field in all of those 400 posts
    that has a phone number.

    Can I make that kind of a php code that says:
    “go to all those posts, cut the forst three digits of the number
    in that custom field and past them to a new custom field
    and give it a name for the new custom field I want it to create for those
    three digits” ?

    Thank’s. I know I’m quite a hassle ??

    Hi again Begin,

    I’m happy to help. You would first want to create your new field in your database to accept the input from the other custom field. Then you could run a SQL query to look for that certain number of characters in that string, remove them and insert them into the new field. You could code that using PHP like keesiemeijer did above. Of course, don’t forget to backup your database before making any changes.

    Thread Starter Begin

    (@bentalgad)

    Hi ChristiNi! Thank you very much, I just don’t really know how to “code” ??

    Can you give me a php code that does that “take from this field to that field” action?

    Thank’s!!!

    Moderator keesiemeijer

    (@keesiemeijer)

    Make a backup of the database first and try it with this

    <?php
    $metakey = 'number';
    $new_meta_key = 'threedigits';
    
    $the_posts = get_posts('meta_key='.$metakey.'&numberposts=-1');
    foreach ( $the_posts as $the_post  ) {
    $number = trim(get_post_meta( $the_post->ID , $metakey, true));
    preg_match('/^[0-9]{3}/', $number, $matches);
    if(is_array($matches) && !empty($matches)) {
    update_post_meta($the_post->ID , $new_meta_key, $matches[0]);
    }
    }
    ?>

    Change the variable $metakey to the telephone number metakey name you already have.
    And change the variable $new_meta_key to the new metakey name you want to use for the three digits.

    Thread Starter Begin

    (@bentalgad)

    Thank’s again keesiemeijer!

    It did copy the 3 digits to the new custom field,
    but didn’t delete them from the phone custom field… ?!

    Btw – I need to delete a “-” from that phone custom field
    (The number is in a format like that: 052-3454435,
    so I need to delete all the “052-” part…)

    Moderator keesiemeijer

    (@keesiemeijer)

    Make a backup of the database first and try it with this

    <?php
    $metakey = 'number';
    
    $the_posts = get_posts('meta_key='.$metakey.'&numberposts=-1');
    foreach ( $the_posts as $the_post  ) {
    $number = trim(get_post_meta( $the_post->ID , $metakey, true));
    preg_match('/^[0-9]{3}-/', $number, $matches);
    if(is_array($matches) && !empty($matches)) {
    $deleted_digits = preg_replace('/^[0-9]{3}-/','',$number);
    update_post_meta($the_post->ID , $metakey , $deleted_digits);
    }
    }
    ?>

    Change the variable $metakey to the telephone number metakey name you already have.

    Thread Starter Begin

    (@bentalgad)

    What does that code suppose to do?

    Moderator keesiemeijer

    (@keesiemeijer)

    Get rid of the first three digits and a “-“.
    if the phone custom field is “052-3454435” it will be updated to “3454435”

    Thread Starter Begin

    (@bentalgad)

    Yay! It Did! Thank you!Thank you!Thank you!Thank you!Thank you!

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘advanced batch custom field editing’ is closed to new replies.