Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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 :)

share|improve this question
1  
You could create a new column in the user meta table to store an array of those arrays with the respective workout IDs in it. –  t f Mar 23 '13 at 12:11
    
@tf That would be my first choice as well. Why not add this as an answer? –  Johannes Pille Mar 23 '13 at 13:02
    
@JohannesPille I didn't post an actual answer, because right now I lack the time to provide some code of a working example (which might be useful or even neccessary for the asker, depending on his/her knowledge/experience). So feel free to elaborate an answer. :) –  t f Mar 23 '13 at 13:24
    
@tf cool idea, I hadn't thought of it that way! I'd been approaching this from a page centred approach instead of a user centred approach. Do you think that a column in the user meta table would be the best way to do this if a user was to create and save, say, 20 workout variations? Would love to hear more of your approach. –  rbcpt Mar 23 '13 at 13:41
1  
@s_ha_dum I was under the assumption that the "new column" was simply a misuse of the word by the original commenter. Obviously adding the exercise plans as key/value pairs is sufficient. You ain't missing anything. –  Johannes Pille Mar 23 '13 at 15:23
show 3 more comments

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
1  
Great breakdown, more than enough to be dangerous with. Thanks! –  rbcpt Mar 24 '13 at 0:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.