wp_usermeta wp_usersettings

When a user changes their admin settings; like the screen options, posts per page or moves metaboxes around or ‘hides’ them, it is saved in their user settings. So if user settings are blank – the user has never tailored their admin environment. Aside: (I use this to great advantage in one of my plugins … Read more

Using GROUP CONCAT in my-sql query with wp_usermeta table

How about a Pivot Query? This would return rows that have first_name,last_name,user_login. You could then add whatever condition you wanted using a WHERE clause. SELECT MAX(CASE WHEN wp_usermeta.meta_key = ‘first_name’ then wp_usermeta.meta_value ELSE NULL END) as first_name, MAX(CASE WHEN wp_usermeta.meta_key = ‘last_name’ then wp_usermeta.meta_value ELSE NULL END) as last_name, wp_users.user_login FROM wp_users LEFT JOIN wp_usermeta … Read more

Incrementally add or substract from usermeta field

The function below should do your job. function manage_credits( $operation = ‘add’, $amount = 0 ) { $user_id = get_current_user_id(); if ( $user_id ) { $user_credit = (int) get_user_meta( $user_id, ‘my_user_credit’, true ); if ( ‘add’ == $operation ) { $user_credit += $amount; //For adding credits } elseif ( ‘remove’ == $operation && ( $user_credit … Read more

Custom User Field in Dashboard Widget

There’s get_the_author_meta() for such a task. (get_* functions normally don’t echo/print the output – hence the name). // Both values are *optional* get_the_author_meta( $field, $user_id ); Normally it’s only meant to be used inside a loop to get the data of the posts author, therefore it internally uses global $authordata;. But, you can also throw … Read more

Sorting custom post type by usermeta

You have to add meta_key, orderby and order parameters to your query as described in the Codex: Order & Orderby Parameters: <?php $args = array( ‘post_type’ => ‘teacher’, ‘paged’ => $paged, ‘meta_key’ => ‘last_login’, ‘orderby’ => ‘meta_value’, ‘order’ => ‘DESC’ ); $my_custom_query = new WP_Query($args); Also note I don’t use $wp_query variable to store the … Read more

How to save data of an input field to an array

Don’t use update_usermeta, it is deprecated, update_user_meta is the one to use. You get the previously saved value out with get_user_meta. <input type=”text” name=”group[]” id=’group[]’ class=”regular-text” value=”<?php echo esc_attr( get_user_meta( $user->ID, ‘group’, true ) ); ?>” />