无法正确获取user_meta

时间:2013-10-03 作者:bestprogrammerintheworld

我有以下代码:

function get_user_fields($user, $output = true, $main_title = \'Anmäld till kurs/kurser\', $course_update = false) {   
    if ($output === true) {
        if (strlen($title)>0) {
            echo \'<h3>\' . $main_title . \'</h3>\';
        }
    }

    //Get locations (course-categories taxonomy) that starts with kurser_ in it\'s permalink (slug)
    $locations = get_categories_with_slug(\'kurser_\');

    //Get list of all categorycourses
    $categorycourses = get_terms(\'categorycourses\');

    $save_meta = array(); //When no output, use function for storing metadata-array to save
    foreach($locations as $location) {
        if ($output === true) {
            echo \'<h3 class="location">\'.$location->name.\'</h3>\';            
        } 

        foreach($categorycourses as $categorycourse) {
            //When a coursecategory has location as it\'s parent, then show courses for category
            if ($categorycourse->parent == $location->cat_ID) {
                wp_reset_query();
                $args = array(\'post_type\' => \'course\',
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'categorycourses\',
                            \'field\' => \'slug\',
                            \'terms\' => $categorycourse->slug,
                        ),
                    ),
                 );

                 $loop = new WP_Query($args);
                 if($loop->have_posts()) {
                    $course_field_name = \'course_\' . get_the_ID();

                    //Check when updating course                           
                    if ($course_update === true) {
                        echo \'course update is true\';
                        //Course is selected by user in db, is it set as reserve?

                        if (get_user_meta($user->ID, $course_field_name) == 1) {
                            echo \'course field name is set\';
                            //User is selected as reserve for this course in db
                            if (get_user_meta($user->ID, $course_field_name.\'_reserve\') == 1) {
                                 echo \'course field reserve is 1\';
                                //Can user be selected as non-reserve?
                                //(course is not full any longer)
                                $nr_available = intval(get_field(\'nr_available\'));
                                                            echo \'nr available = \' . $nr_available;

                                if ($nr_available > 0) {
                                       //User is no longer reserve for this course, save info to db
                                        update_usermeta($user->ID, $course_field_name, 0);  //FOR TESTING
                                       update_usermeta($user->ID, $course_field_name.\'_reserve\', 0);  
                                }
                            }
                        }
                    }

                    if ($output === true) {                        
                        echo \'<h4 class="rubrik">\'.$categorycourse->name.\'</h4>\';                   
                        echo \'<table class="form-table courses" cellspacing="0" cellpadding="0"><thead>\';
                        echo \'<tr><th>Kursnamn</th><th>Startvecka</th><th>Veckodag</th><th>Starttid</th><th>Pris</th><th>Antal platser</th>\';
                        echo \'</tr>\';
                        echo \'</thead>\';
                        echo \'<tbody>\';
                    }

                    while($loop->have_posts()) : $loop->the_post();
                        $nr_available = intval(get_field(\'nr_available\'));
                        $status_available_full = 0;
                        if ($nr_available == 0) {
                            $nr_message = \'fullt\';
                            $status_available_full = 1;
                        }   
                        else if ($nr_available<=4) {
                            $nr_message = \'fåtal platser\';
                        }  
                        else {
                            $nr_message = \'platser finns\';
                        }

                        if ($output === true) {                                                                                    
                            $checked = get_user_meta($user->ID, \'course_\' . get_the_ID(), true); 
                            if ($checked == 1) {
                                $checked = \' checked="checked"\';                              
                            }
                            else {
                                $checked = \' \';
                            }
                            echo \'<tr>\';
                            echo \'<td class="title course-name">\';
                            echo \'<input type="checkbox"\' . $checked . \'name="course_\' . get_the_ID() . \'">&nbsp;\' . get_the_title();
                            //If course is full then be able to book pupil as reserve
                            if ($status_available_full === 1) {
                                echo \'<span class="reserve">(reserv)</span>\';
                             }
                            echo \'</td>\';                       
                            echo \'<td class="title start-week">\' . get_field(\'start_week\') . \'</td>\';                                                        
                            echo \'<td class="title week-day">\' . get_field(\'week_day\') . \'</td>\';
                            echo \'<td class="title start-time">\' . get_field(\'start_time\') . \'</td>\';  
                            echo \'<td class="title price">\' . get_field(\'course_cost\') . \'</td>\';

                            echo \'<td class="title available">\' . $nr_message . \'</td>\';
                            echo \'</tr>\';
                        }
                        else {
                            //Add info to array when using not output
                            $save_meta[] = array(
                                \'course\' => $course_field_name,
                                \'is_reserve\' => $status_available_full
                            );   

                        }                        
                    endwhile;
                    if ($output === true) {
                        echo \'</tbody>\';
                        echo \'</table>\';
                    }
                 }             
            }

        } //End foreach categorycourses

    } //End foreach locations

    //Return array when no output is done
    if ($output !== true) { 
        return $save_meta;
    }
}

/* SAVE EXTRA FIELDS */

/* helper-function to save extra fields */
function save_user_fields($user_id) {
    if ( !current_user_can( \'edit_user\', $user_id ) )
    return false;

    //Save values of checkbox-fields into db
    $save_meta = get_user_fields($current_user, false);  //False tells that no output should be done  

    //Go through all courses and save them
    foreach($save_meta as $current_meta) {
        $course = $current_meta[\'course\'];
        $is_reserve = $current_meta[\'is_reserve\'];
        $update = 0;
        if (isset($_POST[$course])) {
            $chkbox = $_POST[$course];
            if ($chkbox == \'on\') {
                $update = 1;
            }
        }
        //Save values from form into db
        update_usermeta($user_id, $course, $update);                    //Save course into db with format course_{id of course)
        update_usermeta($user_id, $course . \'_reserve\', $is_reserve);  //Save if user is reserve (1) or not (0) for this course
    }
}

//Hooks for user (Update/add)
add_action(\'personal_options_update\', \'save_user_fields\');
add_action(\'edit_user_profile_update\', \'save_user_fields\');
add_action(\'user_register\', \'save_user_fields\'); //When adding new users
我使用函数save\\u user\\u fields将用户的元数据保存到数据库中。我不确定我能不能用$current_user 此函数中的变量?(调用get\\u user\\u字段时)

基本上,代码是使用复选框在userprofile中生成自定义分类类别的列表,并且在更新用户配置文件时,复选框的值(类别是否选中)保存到db中。这似乎奏效了。

我正在将此元数据保存在save_user_fields()

//Save course into db with format course_{id of course) (1=checked, 0 = unchecked)
update_usermeta($user_id, $course, $update);           

//Save if user is reserve (1) or not (0) for this course
update_usermeta($user_id, $course . \'_reserve\', $is_reserve);  
I wonder why the code does not get past the statement (in get_user_fields-function, beginning of "the loop") if (get_user_meta($user->ID, $course_field_name) == 1) { ?

它不会回显“课程字段名称已设置”

我还使用插件高级自定义字段,因此我使用get_field()

2 个回复
最合适的回答,由SO网友:bestprogrammerintheworld 整理而成

我最终解决了这个问题:

第一个问题是向函数发送了错误的用户对象(实际上只是一个空字符串)Solved it by writing this:

function save_user_fields($user_id) {
    if ( !current_user_can( \'edit_user\', $user_id ) )
    return false;

    //Save values of checkbox-fields into db
    $current_user = new WP_User($user_id);
    $save_meta = get_user_fields($current_user, false);  //False tells that no output should be done  

    //Go through all courses and save them
    foreach($save_meta as $current_meta) {
        $course = $current_meta[\'course\'];
        $is_reserve = $current_meta[\'is_reserve\'];
        $update = 0;
        if (isset($_POST[$course])) {
            $chkbox = $_POST[$course];
            if ($chkbox == \'on\') {
                $update = 1;
            }
        }
        //Save values from form into db
        update_usermeta($user_id, $course, $update);                    //Save course into db with format course_{id of course)
        update_usermeta($user_id, $course . \'_reserve\', $is_reserve);  //Save if user is reserve (1) or not (0) for this course
    }
}
Next issue:在Karine的帮助下(THANKS!) 答案我在get\\u user\\u meta()中使用true,因为我想要meta\\u user\\u表中字段的实际值,而不是数组。

if ($course_update === true) {

    //Course is selected by user in db, is it set as reserve?
    if (intval(get_user_meta($user->ID, $course_field_name, true)) == 1) {
        //User is selected as reserve for this course in db
        if (get_user_meta($user->ID, $course_field_name.\'_reserve\', true) == 1) {
            //Can user be selected as non-reserve?
            //(course is not full any longer)
            $nr_available = intval(get_field(\'nr_available\'));

            if ($nr_available > 0) {
                   //User is no longer reserve for this course, save info to db
                   update_usermeta($user->ID, $course_field_name.\'_reserve\', 0);  
            }
        }
    }
}
And now the issue that made me completly mad, 即使我早就应该看到这个。。。上面的代码片段用于设置/获取有关课程的信息(并保存元数据数组),但实际上NOT 在回路内部。是在

if($loop->have_posts()) {
$course_field_name = \'course_\' . get_the_ID();
对于储量和可用nr\\u的实际比较,也存在同样的问题,因此我只是将该块移动到循环中,如下所示(并做了一些小的更改):

 while($loop->have_posts()) : $loop->the_post();
    $course_field_name = \'course_\' . get_the_ID();

     //Check when updating course                           
    if ($course_update === true && $course_id == get_the_ID()) {

        //Course is selected by user in db, is it set as reserve?
        if (intval(get_user_meta($user->ID, $course_field_name, true)) == 1) {
            //User is selected as reserve for this course in db
            if (get_user_meta($user->ID, $course_field_name.\'_reserve\', true) == 1) {
                //Can user be selected as non-reserve?
                //(course is not full any longer)
                $nr_available = intval(get_field(\'nr_available\'));

                if ($nr_available > 0) {
                       //User is no longer reserve for this course, save info to db
                       update_usermeta($user->ID, $course_field_name.\'_reserve\', 0);  
                }
            }
        }
    }

SO网友:Karine

也许你可以试试get_user_meta($user->ID, $course_field_name, true) ?默认情况下,get_user_meta() 返回数组

看见http://codex.wordpress.org/Function_Reference/get_user_meta

结束

相关推荐