Skip Navigation

[Resolved] custom field value update with custom code

This thread is resolved. Here is a description of the problem and solution.

Problem:

The user needs to update the 'book-url' custom field value for all posts of the 'book' post type without having to edit each post individually.

Solution:

You can use a code like this:

function update_book_urls($post_type, $url) {
    // Get all posts of the 'book' post type
    $args = array(
        'post_type' => $post_type,
        'posts_per_page' => -1,
    );
    $book_posts = get_posts( $args );

    // Loop through each post and update the 'book-url' custom field
    foreach ( $book_posts as $post ) {
        update_post_meta( $post->ID, 'wpcf-saas-deal-link', $url );
    }
}

update_book_urls('saas', 'https://book.com');

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by fahimS-2 1 year, 10 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2573601

Let's say I have a custom post type called 'book' and its custom field is 'book-url'. Now I want to set the value of 'book-url' to 'hidden link' for all of the posts of 'book' post type. But I don't want to go to all of the book posts one by one and change it. Because there are thousands of posts in book posts. So it will be very time consuming. Also, as far as I know, Toolset Types doesn't have any API.

So I need a custom code which will change the value of 'book-url' of all the book posts at once. I will add the custom code to toolset and when I will Run it, it will perform the task. Can you kindly give me the code?

#2573663

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

You can use the code below:

function update_book_urls() {
    // Get all posts of the 'book' post type
    $args = array(
        'post_type' => 'book',
        'posts_per_page' => -1,
    );
    $book_posts = get_posts( $args );

    // Loop through each post and update the 'book-url' custom field
    foreach ( $book_posts as $post ) {
        update_post_meta( $post->ID, 'wpcf-book-url', '<em><u>hidden link</u></em>' );
    }
}

add_action( 'init', 'update_book_urls' );

This code defines a function called update_book_urls() that gets all posts of the book post type and updates the wpcf-book-url custom field to "hidden link". The function is then added as a callback to the init action using the add_action() function.

This way, whenever the init action is fired, the update_book_urls() function will be executed and update the wpcf-book-url custom field for all book posts.

After that, just load the website homepage once. And then all the values will be the URL in question, even the ones that have other value.

Now you can remove the code from functions.php so that you can start adding other values if you want.

Do not run this code without having a backup of your database.

Please consider that all Toolset custom fields have wpcf- prefix on them when you want to use a custom code to retrieve them.

Thanks.

#2574023
Screenshot_4.jpg

I don't want to load the homepage to update the custom field. Rather I want to run the code using the Run Now option in Custom Code.
So I changed a few things in the code.

function update_book_urls($post_type, $url) {
    // Get all posts of the 'book' post type
    $args = array(
        'post_type' => $post_type,
        'posts_per_page' => -1,
    );
    $book_posts = get_posts( $args );
 
    // Loop through each post and update the 'book-url' custom field
    foreach ( $book_posts as $post ) {
        update_post_meta( $post->ID, 'wpcf-saas-deal-link', $url );
    }
}
 
update_book_urls('saas', '<em><u>hidden link</u></em>');

Will this code work?

#2574041

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Yes it should work. Please have a backup before testing.

Thanks.

#2574165

My issue is resolved now. Thank you!