Wordpress function to update 6,000+ users gets 502'd. Need MYSQL query alternative.
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Need to concoct the data from several ACF repeater fields to a single ACF repeater for all our users (6,000+)

Trying to run function on init once and then I would delete it from source after success.
Host's 502 kills my operation though and only half our users get their data converted.

Here's the current code:

function process_post() {

global $wpdb; //reference database

$userArgs = array('fields' => 'ID'); 
$my_users = get_users( $userArgs ); //Returns all user IDs as an array

foreach ( $my_users as $my_user ) { //For each user do...

    //This is the new repeater. We determine whether there's already field data or to create a new array.
    $field_key = "field_5601d54ae69c7";
    if ( get_field($field_key, 'user_' . $my_user ) ){
        $value = get_field($field_key, 'user_' . $my_user );
    } else {
        $value = array();

    //Search database for the old repeater values
    $rows = $wpdb->get_results($wpdb->prepare(
        SELECT * 
        FROM {$wpdb->prefix}usermeta
        WHERE meta_key LIKE %s
        AND user_id = %s
        'course_history_%_course_id', // meta_name: $ParentName_$RowNumber_$ChildName
    if( $rows ) { //If, and then for each row of data found...
        foreach( $rows as $row ) {
            $value[] = array("course" => get_the_title(1522), "id" => $row->meta_value, "parent" => 1522 ); //Add to the array created earlier (1522 is a parent post - irrelevant)
        update_field($field_key, $value, 'user_' . $my_user ); //Concoct to the new repeater field


add_action( 'init', 'process_post' );

I would like a MYSQL query alternative to the above. Is this possible?

Thank you in advance.

Do you have shell access to your server?
ZeroCool over 4 years ago
No, the client I'm working with is on a shared host.
benrosati over 4 years ago
How about sorting the that users array then create a %_GET var to select the nth 500 or 1000 users. Basically break up this operation into smaller chunks.
weex over 4 years ago

Crowdsource coding tasks.

0 Solutions