How can I save an array from a random post sequence for later use?

My suggestion is to store the workouts as a new entry inside the particular user’s meta data.

» Saving a single workout

Suppose the workout consists of the exercise IDs 1, 3, 5, 9. Then the code is:

// this is the current workout
$workout = array(1, 3, 5, 9);

// get the current user
$user = wp_get_current_user();

// update the user's meta data
update_user_meta($user->ID, 'saved_workout', $workout);

Basically, that’s it.

However, if you want the user to be able to store more than one workout, you might want to get the current one(s), then add the new one, and finally update the data. This could look like the following:

» Saving multiple workouts

// this is the current workout
$workout = array(1, 3, 5, 9);

// get the current user
$user = wp_get_current_user();

// get saved workout(s), ...
$saved_workouts = get_user_meta($user->ID, 'saved_workouts');
if (! $saved_workouts)
    $saved_workouts = array();
// ... add current workout ...
$saved_workouts[] = $workout;
// ... and update the user's meta data
update_user_meta($user->ID, 'saved_workouts', $saved_workouts);

Once again, this is just the basic idea.

Perhaps you might want to give the user some means to access the saved workouts, and maybe edit/delete etc.

Leave a Comment