Reputation: 146
I have created a checkbox list of all my customers in wordpress.
The data is stored into my custom table called wp_assigned_products, I get the post_id(product id), meta_key(customer keyword), meta_value(customer ID) and assigned(yes or no) values saved into the table when a user is is checked and the post is updated.
I cant for the life of me figure out how to return a checked box for the checked users, once the page is reloded.
My checkboxes are all empty but the record is saved to the database table. Can anyone give me a push in the right direction? I have been working on this for hours but keep ending up with a conflict between the meta_value and assigned row. Many thanks.
My code as follows:
add_action("admin_init", "users_meta_init");
function users_meta_init(){
add_meta_box("users-meta", "Assign customers to this product", "users", "product", "normal", "high");
}
function users(){
global $post;
$custom = get_post_custom($post->ID);
$user_args = array(
'role' => 'customer',
'orderby' => 'display_name'
);
$wp_user_query = new WP_User_Query($user_args);
$authors = $wp_user_query->get_results();
if (!empty($authors)) {
foreach ($authors as $author) {
$author_info = get_userdata($author->ID);
$author_id = get_post_meta($post->ID, 'users', $author_info->ID);
echo " <input type='checkbox' name='users' value='$author_info->ID'> $author_info->first_name $author_info->last_name<br/> ";
}
}
}
add_action('save_post', 'save_userlist');
function save_userlist($author_id){
global $wpdb;
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post->ID;
return $author_info->ID;
}
$table_name = $wpdb->prefix . "assigned_products";
$wpdb->insert($table_name, array(
'post_id' => $post->ID,
'meta_key' => "customer",
'meta_value' => $_POST["users"],
'assigned' => "yes",
)
);
}
Upvotes: 1
Views: 110
Reputation: 94672
I am not a WP expert by any stretch of the imagination but there are a few obvious woopsees in this code.
This cannot work, only the first return will work, I am surprised you are not getting a compiler warning about unreachable code? Which makes me think you are not looking at your error log!
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post->ID;
return $author_info->ID;
}
Also within the scope of that function there is no $author_info
object to return $author_info->ID
anyway.
This IF looks a little off!
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
if DOING_AUTOSAVE
is defined what is && DOING_AUTOSAVE
supposed to be testing, do you mean
if (defined('DOING_AUTOSAVE') && $something == DOING_AUTOSAVE) {
Finally the way to set a checkbox to checked is by adding the property checked="checked"
its attribute list
echo " <input checked="checked" type='checkbox' name='users' value='$author_info->ID'> $author_info->first_name $author_info->last_name<br/> ";
But I assume you only want to do that IF something == something and that is where my lack of WP knowledge means I cannot see what you might be testing for or against.
Upvotes: 1