WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] advanced batch custom field editing (17 posts)

  1. Begin
    Member
    Posted 2 years ago #

    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!

  2. ChristiNi
    Member
    Posted 2 years ago #

    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!

  3. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  4. Begin
    Member
    Posted 2 years ago #

    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... ?!

  5. keesiemeijer
    moderator
    Posted 2 years ago #

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

  6. Begin
    Member
    Posted 2 years ago #

    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!

  7. keesiemeijer
    moderator
    Posted 2 years ago #

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

  8. Begin
    Member
    Posted 2 years ago #

    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 :-)

  9. ChristiNi
    Member
    Posted 2 years ago #

    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.

  10. Begin
    Member
    Posted 2 years ago #

    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!!!

  11. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  12. Begin
    Member
    Posted 2 years ago #

    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...)

  13. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  14. Begin
    Member
    Posted 2 years ago #

    What does that code suppose to do?

  15. keesiemeijer
    moderator
    Posted 2 years ago #

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

  16. Begin
    Member
    Posted 2 years ago #

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

  17. keesiemeijer
    moderator
    Posted 2 years ago #

    No problem. Glad it worked.

Topic Closed

This topic has been closed to new replies.

About this Topic