how i can get the users nice name in a function?
currently i got it with an avatar, but dont know hot to get the users nice name:
$table = $wpdb->prefix . "thumbsup_info";
$result = $wpdb->get_results("SELECT userid FROM $table WHERE postid = '$postid' LIMIT 10", ARRAY_A);
$total_user = $wpdb->num_rows;
for($i=0; $i <$total_user ; $i++)
{
$userid.= get_avatar($result[$i]['userid'],100);
}
return $userid;
Get the user object with the following line of code:
$user = get_user_by( 'id', $result[$i]['userid'] );
Then you can just access the property you need. To get the nicename for example you would use $user->data->user_nicename
Related
I need to change the user rol when a user visit any page of my site based in the value of data in database.
In my DB in the wp_usermeta has this row:
The meta_value can take the values 1 and 0.
Then, I need to add a hook to be fired when any page of the site is loaded to run a code which will change the user role to "myCustomRole" when the 'meta_value' of 'meta_key' is '1'. Something like:
add_action('template_redirect', 'hooker');
function hooker(){
$id_logged_user = $current_user_id = get_current_user_id();
$table_name = "wp_usermeta";
$results = $wpdb->get_results( "SELECT * FROM $table_name WHERE 'user_id' == $id_logged_user && 'meta_value' == 1 && 'meta_key'== 'wpuef_cid_c17');
if($results) {
$wp_user_object = new WP_User($current_user->ID);
$wp_user_object->set_role('my_custom_role');
}
}
init hook is better to update user role.
add_action('init', 'changeUserRole');
function changeUserRole()
{
$user_id = get_current_user_id();
$user_meta = get_user_meta($user_id, 'wpuef_cid_c17', true);
if ($user_meta == 1) {
$user = new WP_User( $user_id );
$user->set_role( 'my_custom_role' );
}
}
I'm looking for a solution in $wpdb->get_results() which is equivalent PDO::FETCH_NUM.
This is what I'm trying to achieve:
function get_data( $token, $field ){
global $wpdb;
$wpdb->show_errors();
$result = $wpdb->get_results('SELECT '.$field.' FROM ' . $this->get_table(). ' WHERE token =\''.$token.'\' ORDER BY id DESC');
return $result[0];
}
As you can see from my query above, I'm selecting specific columns from the database, but I don't know how to access selected column using $wpdb->get_results();
I would like to get the value of the $field column based on the where clause criteria.
After deep research I have gotten a solution.
Basically, this is how to get the $field column.
function get_data( $token, $field ){
global $wpdb;
$wpdb->show_errors();
$result = $wpdb->get_results('SELECT '.$field.' FROM ' . $this->get_table(). ' WHERE token =\''.$token.'\' ORDER BY id DESC');
$data = "";
foreach ($result as $key => $value) {
$data = $value->$field;
}
return $data;
}
To make it simple, let's assume you have a column called first_name, this is how to get the first_name column from the database.
function get_data( $token ){
global $wpdb;
$wpdb->show_errors();
$result = $wpdb->get_results('SELECT first_name FROM your_table_name WHERE token =\''.$token.'\' ORDER BY id DESC');
foreach ($result as $key => $value) {
echo $data = $value->first_name;
}
}
Hi I am using learndash Wordpress plugin. I want to get the data related to a user tht how many courses he is enrolled in and how many has he completed. Is there a way to check this? does learndash provide any solution for this or should I query data myself?
Any help is appreciated. Thanks in advance.
Please ask for any more details if you want.
You can use the following to get all course ID's the current user is currently enrolled to:
learndash_user_get_enrolled_courses(get_current_user_id())
I found that all is stored inside the table learndash_user_activity and you can query that table to get user stats.
For example I get the list of the in-progress users for a given course with the following query:
public static function get_inprogress_users_for_course( $course_id )
{
global $wpdb;
if( empty( $course_id ) ) return [];
$results = $wpdb->get_results( "SELECT `user_id` FROM `" . $wpdb->prefix . "learndash_user_activity` "
."WHERE `course_id` = '" . intval( $course_id ) . "' "
."AND `activity_type` = 'lesson' "
."GROUP BY `user_id`" );
return $results;
}
In the same way, you can get all the IDs of the users ever enrolled to a course changing activity_type from 'lesson' to 'course' in the query, and if you want to only get enrolled course by a user, you can add the user_id to the query, like this:
public static function get_courses_for_user( $user_id )
{
global $wpdb;
if( empty( $course_id ) ) return [];
$results = $wpdb->get_results( "SELECT * FROM `" . $wpdb->prefix . "learndash_user_activity` "
."WHERE `user_id` = '" . intval( $user_id ) . "' "
."AND `activity_type` = 'course' "
."GROUP BY `course_id`" );
return $results;
}
I know this is not exactly what you were searching for, but it could still be useful.
You can return anything using wp_query. Try this:
function wpso49370180_get_course_name($courseid) {
global $wpdb;
$user_id = the_author_meta( 'ID' ); //alt method below
$query_course = "SELECT post_title
FROM wp_posts
WHERE post_type = 'sfwd-courses'
AND post_status NOT IN ( 'trash','auto-draft','inherit' )
AND post_author='$user_id' LIMIT 10";
return $wpdb->get_var($query_course);
}
You will need to either know the user_id or get it from the post (sfwd-quiz, sfwd-course, sfwd-lesson) -see below.
The data you want can be all (*) You will have to do a meta_query if you want deeper data that is not in the post_type tables.
/**
* Gets the author of the specified post. Can also be used inside the loop
* to get the ID of the author of the current post, by not passing a post ID.
* Outside the loop you must pass a post ID.
*
* #param int $post_id ID of post
* #return int ID of post author
*/
function wpso49370180_get_author( $post_id = 0 ){
$post = get_post( $post_id );
return $post->post_author;
}
I have a plugin with stores user activity in the table wp_usermeta and under meta_key user_last_activity and meta_value something like a:12:{} where 12 is the user id.
So, If I was logged in as user with id: 12 my current page would run this code:
$user = get_user_id();
$activity = get_user_meta($userid, 'user_last_activity', true);
var_dump($activity);
so, that's pretty much it. Except, if I was logged as admin, I would like to see ALL activity by all users.
So, is there anyway to get all user meta? currently I am blindingly doing
foreach(range(1, 1000) as $value){
$activity = get_user_meta($value, 'user_last_activity', true);
var_dump($activity);
}
I am offcourse assuming there are 1000 users, but you can see the limitations.
I would advise to do a single query for the user_meta instead of running get_user_meta within a foreach-loop.
global $wpdb;
$results = $wpdb->get_results("
SELECT user_id, meta_value as 'user_last_activity'
FROM $wpdb->usermeta
WHERE `meta_key` = 'user_last_activity'
");
var_dump($results);
You can use the get_users() function to get all of the users on your site, and then loop through those to grab the user activity.
$users = get_users(); // get array of WP_User objects
foreach ( $users as $user ) {
$activity = get_user_meta( $user->ID, 'user_last_activity', true );
var_dump( $activity );
}
I'm trying to resolve the following -
$result = $wpdb->get_results( "SELECT * FROM table name WHERE ID = 1" );
foreach ( $result as $print ) {
print '<div class = "facebook">facebook link</div>';
}
This should display the facebook link and link towards the correct page but unfortunately doesn't work as it removes the link.
I hope i'm not doing this completely wrong as I used a foreach loop as I will be adding other social media.
You have two choices:
Use get_results() as you did:
$results = $wpdb->get_results( "SELECT * FROM $wpdb->table_name WHERE id = 1");
foreach ( $results as $result )
{ echo $result->column_name }
OR
Use get_rows():
$mylink = $wpdb->get_row( "SELECT * FROM $wpdb->table_name WHERE link_id = 10", ARRAY_A );
echo $mylink['column_name'];
You can see more here: https://codex.wordpress.org/Class_Reference/wpdb
The link is just not visible because your a tag doesn't contain any text.
Change this:
<div class = "facebook">facebook link</div>
to this:
<div class = "facebook">facebook link</div>
(move the text inside of the a tag).
Have you used global $wpdb ?
Use it like this:
global $wpdb;
$result = $wpdb->get_results( 'SELECT * FROM $wpdb->table name WHERE ID = 1');
Check this official doc for more info.