I’m not big on SQL but could you not put all of your user id’d in and array and run a foreach loop on that array with the command you used above?
Something like:
$users = array( 12,23,45,56); //array of user id's
global $wpdb;
foreach ( $users as $user ) {
$query="INSERT INTO XXXXXX_bp_xprofile_data (field_id, user_id, value, last_updated) VALUES ( fid, $user, 3, date )";
$results = $wpdb->get_results($query);
}
Where fid = the field number that you want to add and date is the timestamp for when the ddata was added.
Hello
Thank you for this idea
So I have to go through php.
If this code works, can I extend it to more than a thousand members?
Regards
I’d be tempted to do it in batches but yes it should work, probably not the most efficient way of doing it
Note that this assumes the field does not already exist for the affected users, which should be correct.
Also clear WordPress object cache after manipulating the DB directly.
Hello
@venutius
What I meant was that I can not create a big array!
Is not it rather necessary to create a loop from the 1st to the last member?
@djpaul
why? by program
Thanks
Then what you’d need to do is user SQL to pull off all the user_ids and put them into the $users variable and quirt that into the foreach loop.
But you’d also have to add an if statement around the db INSERT in order to make sure each user only had the field once.
So I think your function would look something like this, I’ve not tested this and like I say I’m no expert on $wpdb queries:
global $wpdb;
$users = $wpdb->get_results("SELECT ID from $wpdb->users");
foreach ( $users as $user ) {
$existing_field=$wpdb->get_results("SELECT fid FROM $wpdb->bp_xprofile_data for user_id = $user");
if ( !$existing_field ) {
$query="INSERT INTO $wpdb->bp_xprofile_data (field_id, user_id, value, last_updated) VALUES ( fid, $user, 3, date )";
$results = $wpdb->get_results($query);
}
}`
Hello @venutius
I tested the first version that works well. Thank you
I am now going to try to test the other. I did not understand to empty the cache, whether it was with BO programming or a cache plugin
$users = array(3809,2749,2430,3849); //array of user id's
global $wpdb;
$note_champ="794";
$note_valeur="3";
foreach ( $users as $user ) {
$query="INSERT INTO wpamo_bp_xprofile_data (field_id, user_id, value, last_updated) VALUES ($note_champ, $user, $note_valeur, NOW() )";
$results = $wpdb->get_results($query);
}
regards
Because the object cache caches query results from the database and if you modify the database – either directly or using WP’s DB APIs – you to clear it. Otherwise you’ll end up with inconsistent results.
Using WP-CLI is probably easiest for this: https://developer.wordpress.org/cli/commands/cache/flush/
If you don’t know enough to write these PHP commands yourself, using some shell scripting and the BuddyPress WP-CLI commands will probably be easier: https://github.com/buddypress/wp-cli-buddypress/
Hi @djpaul
In my case, it’s a new field with a new value.
nothing was cached before !!
But, I ask a new question for new members who will register after the execution of the program. I do not see a way to put a default value in a numeric field. Did I look bad?
Regards
Hello,
On another forum, I had this suggestion with WP function 🙂
But I am embarrassed if I can not put a default value to my numeric field!
$users = get_users( [ 'fields' => 'ids' ] );
global $wpdb;
/** @var \WP_User $user */
foreach ( $users as $user_id ) {
$results = $wpdb->insert(
'bp_xprofile_data',
[
'field_id' => 794,
'user_id' => $user_id,
'value' => 4,
'last_updated' => new \DateTime()
],
[ '%d', '%d', '%d', '%s' ]
);
}
regards