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

I’m building a random workout generator on WordPress to help with my fitness, and am stuck at saving random post arrays for later reference.

I’ll explain.

Read More

The generator should work in two main stages:

  1. You choose how hard you want the workout to be, and how long it should take.

    These values are passed to a new page via the a URL String. This creates a random workout (code at bottom of question) by displaying random posts based on the parameters from the string and code on a dynamic page (‘/your-workout/’)

  2. If you like the workout, I want you to be able to save this random array of posts to use it again later.

    1. The ID’s of the post displayed in this random sequence are saved to an array.
    2. When a user wants to save a workout to use again later, this array of Post IDs is used to create a new WordPress Page, with content populated by those specific posts in that specific order. This would be a child page of (‘/your-workout/’) created by ‘saving’ a workout.

Step 2 is specifically where I am stuck.

How can I save the post IDs of a random array and use them to create a new child page which displays this specific sequence?

Relevant Code for Step 1:

        <?php global $post;
        if (isset($_GET['dm'])) { //Check DM is set
        $mode = $_GET['dm'];      //Get DM Value - will define CPT's taxonomy
        }
        if (isset($_GET['e'])) {  //Check E is set
        $posts = $_GET['e'];      //Get E value - will define how many posts are shown
        }
        $my_query = new WP_Query( "post_type=bw-exercise&orderby=rand&difficulty-mode=$mode&showposts=$posts");?> //Define query using pre-set and URL String values

        <?php   if ( $my_query->have_posts() ) { 
        while ( $my_query->have_posts() ) { 
               $my_query->the_post();?>
        <div class="workout">
            <div class="workout-reps"><?php echo get_post_meta(get_the_id(), "wpcf-reps", true);?></div>
            <div class="workout-info"><?php the_title('<div class="workout-name">', '</div>');?>
            <div class="workout-desc"><?php the_content();?></div>
            </div>
            <div class="workout-link"><a href="<?php the_permalink();?>" target="_blank"><img src="<?php bloginfo('template_url');?>/custom/images/i.jpg"></a></div>
        </div>
        <?php
               }
           }
        ?>
        <?php   wp_reset_postdata();?>

If I can be any clearer, please let me know.

Thanks!

R 🙂

Related posts

Leave a Reply

1 comment

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