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 am trying to make a frontend product editor for WooCommerce, and have a loop that returns all of the products for the current user. I am trying to use gravity forms as the editor, so a single gravity form is displayed and populated for each product. However, I am not sure how to handle naming the function so it is not being re-declared for every product.

global $current_user;
new WP_Query( array( 'author' => $current_user->ID, 'post_type' => 'product' ) );
while ( have_posts() ) {
    the_post();
    add_filter('gfrom_pre_render_7', 'populate_each_product');
    function populate_each_product ($form){
        foreach($form["fields"] as &$field){
            if($field["id"] == 1){
                $field["defaultValue"] = get_post_meta($post->ID, '_virtual', true);
            }
        }
        return $form;
    }
}
wp_reset_query();
?>

Is there someway to add a counter or another variable to the function name? So along the lines of populate_each_product . $post->ID () or something of that nature? I have looked around for this quite a bit, but found no reference of using functions inside loops so I am worried what I am trying to do is not possible

share|improve this question
    
I've modified your question as there was no code indenting of any kind, and it was formatted poorly –  Tom J Nowell Sep 27 '12 at 12:29
    
Also never use query_posts –  Tom J Nowell Sep 27 '12 at 12:30
    
Thank you, I could definitely stand to improve a bit on my formatting, much appreciated –  Kyle Sep 27 '12 at 12:30
    
Oh, I didn't know that about the query_posts, took that off the wordpress codex - thanks –  Kyle Sep 27 '12 at 12:33
    
As useful as the codex is, anyone can edit it, and bad practices can worm their way in, so don't take it as gospel –  Tom J Nowell Sep 27 '12 at 12:56
add comment

2 Answers

up vote 1 down vote accepted

Before continuing, please read this:

http://www.slideshare.net/andrewnacin/you-dont-know-query-wordcamp-netherlands-2012

It's by a core WordPress developer and you should consider it required reading. You should never use query_posts, and it's important to understand how to use loops correctly. That presentation will demonstrate the best practices, how, and why to use them.

I can see the approach you've taken, what you're trying to do is:

foreach post that's of type product that belongs to me
    add a function that fills in the default value for that post

Unfortunately, that filter is fired for all posts, not individual posts. Also your approach is using a named function, if it were javascript and you were using lambdas and it was a per post filter, e.g. 'gfrom_pre_render_'.$post->ID it might work, but that filter doesn't exist, and you're not using lambdas.

So instead, add your function to the filter, and it will get executed on all forms. Then filter out so that it only happens on the ones that you want:

add_filter('gfrom_pre_render_7', 'populate_each_product');
function populate_each_product ($form){
    global $post, $current_user;
    if($post->post_type == 'product' && $post->post_author == $current_user->ID){
        foreach($form["fields"] as &$field){
            if($field["id"] == 1){
                $field["defaultValue"] = get_post_meta($post->ID, '_virtual', true);
            }
         }
    }
    return $form;
}

Also this is how you would have done the query correctly:

$q = new WP_Query( array( 'author' => $current_user->ID, 'post_type' => 'product' ) );
if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
        $q->the_post();
    }
    wp_reset_postdata();
}
share|improve this answer
    
After posting my question I realised that what your question needed was something different, so I rewrote most of it –  Tom J Nowell Sep 27 '12 at 12:54
    
Thanks for taking the time on that Tom, I am a little lost on the implementation of your code, I am probably just missing something. How should I tie the code in to the form? Should I include the form in the loop and the function in the functions.php file? –  Kyle Sep 27 '12 at 14:11
    
You don't need the loop at all, I only included it to demonstrate how it should have been written –  Tom J Nowell Sep 27 '12 at 14:13
    
and yes put the function in functions.php –  Tom J Nowell Sep 27 '12 at 14:14
    
Oh, that's the confusion then about concept and execution. Although I hadn't thought about it how you are doing it. I am trying to place this on a separate page, not on the product page. So in the user dashboard, it will pull a loop of products they have made and and display a copy of the form for each product, populated with the meta. –  Kyle Sep 27 '12 at 14:21
show 4 more comments

Don't add the filter inside the loop. Put the code in functions.php, and use get_posts to get the products and populate the form.

share|improve this answer
    
Thank you for the reply, this is essentially what we ended up doing above. Tom had just offered a more detailed answer. I still have not been able to pull the post ID from the loop on the page in to the function in the functions.php file –  Kyle Sep 27 '12 at 15:46
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.