Query wp.comments by user role - php

Trying to query user comments by specific user role. The idea is to display comments by user group/user role only. The code below shows all existing user comments but doesn't filter by user roles.
$query = $wpdb->prepare( "SELECT *
FROM $wpdb->comments c
LEFT JOIN $wpdb->commentmeta cm ON c.comment_ID = cm.comment_id
LEFT JOIN $wpdb->users u ON c.user_id = u.ID
WHERE c.comment_post_ID = %s
AND c.comment_approved = 1
ORDER BY c.comment_ID $comment_order
",$post->ID);
$comments = $wpdb->get_results($query);
I feel like I need to insert something like this into the above query
ON wp_users.ID = wp_usermeta.user_id
WHERE wp_usermeta.meta_key = 'wp_capabilities'
AND wp_usermeta.meta_value LIKE '%".$user_rol->roles[0]."%'

Related

Combine Two Queries into One?

I have a MySQL query that selects all audioids from a certain user in a subscribe table.
I then have another query which takes this list of audioids and matches them against a field called opids in a table called audioposts. It then selects the titles from that audioposts table and joins the users from a users table at the userid.
Is there a way I can turn these two queries into one query?
query1 = "SELECT audioid FROM subscribe WHERE userid = $userid";
query2 = "SELECT ap.audioid, ap.title, us.name FROM audioposts ap
INNER JOIN audioposts a2 ON a2.audioid = ap.opid
INNER JOIN users us ON us.id = a2.userid
WHERE ap.opid = $newaudio";
Here is the current two query code which I'd like to replace with one query. I've not yet translated this into prepared statements as it's easier for me to visualize it the old-fashioned way. Plus I'll be converting this into NodejS eventually anyway;
$selectaudioid = "SELECT audioid FROM subscribe WHERE userid = $userid";
$audioResult=$dblink->query($selectaudioid);
if ($audioResult->num_rows>0) {
while ($row = $audioResult->fetch_assoc()) {
$newaudio = $row[audioid];
$getallaudio = "
SELECT ap.audioid, ap.title, us.name FROM audioposts ap
INNER JOIN audioposts a2 ON a2.audioid = ap.opid
INNER JOIN users us ON us.id = a2.userid
WHERE ap.opid = $newaudio AND ap.opid <> '0'";
$getallresult = $dblink->query($getallaudio);
if ($getallresult->num_rows>0) {
while ($row = $getallresult->fetch_assoc()) {
$dbdata[]=$row;
}}}}
Just add another join with subscribe.
SELECT ap.audioid, ap.title, us.name
FROM audioposts ap
INNER JOIN audioposts a2 ON a2.audioid = ap.opid
INNER JOIN subscribe s ON s.audioid = ap.opid
INNER JOIN users us ON us.id = a2.userid
WHERE s.userid = $userid AND ap.opid <> '0'

How can i extract administrator from a:1:{s:13:"administrator";b:1;} while fetching from mysql database?

$authors = $wpdb->get_results('SELECT wp_users.user_login, wp_users.display_name, wp_usermeta.meta_value
FROM wp_users INNER JOIN wp_usermeta
ON wp_users.ID = wp_usermeta.user_id
WHERE wp_usermeta.meta_key = "wp_capabilities"
AND wp_usermeta.meta_value LIKE "%'.$args['role'].'%" ORDER BY '.$args['orderby'].' '.$args['order'].' ');
this is my SQL query and I get a:1:{s:13:"administrator";b:1;} but need only administrator.
This should do it, just select your role variable as a constant.
$authors = $wpdb->get_results('SELECT wp_users.user_login, wp_users.display_name, "%'.$args['role'].'%" as role
FROM wp_users INNER JOIN wp_usermeta
ON wp_users.ID = wp_usermeta.user_id
WHERE wp_usermeta.meta_key = "wp_capabilities"
AND wp_usermeta.meta_value LIKE "%'.$args['role'].'%" ORDER BY '.$args['orderby'].' '.$args['order'].' ');

How to display posts from followers and logged-in user in news feed?

I want to display posts from the users being followed and the logged in user using a single PHP MySql query.
I have three tables:
users (user_id, username, ..)
posts (post_id, content, added_by, ..)
Followers (f_id, followed, followed_by)
I have variable
$id = $_SESSION['user_id']
$que = $db->prepare("SELECT posts.*, users.*, followers.follow, followers.followed_by FROM posts
INNER JOIN users ON users.user_id = posts.added_by_user_id
LEFT JOIN followers ON followers.follow = posts.added_by_user_id
WHERE (posts.added_by_user_id = $id OR followers.followed_by = $id) ORDER BY post_id DESC");
$que->execute();
$posts = $que->fetchAll();
But the query shows each post created by logged-in user twice.
You can use any of the following:
Add additional clause to you LEFT JOIN followers and it will do the trick i.e LEFT JOIN followers ON ( followers.follow = posts.added_by_user_id AND followers.followed_by = $id ) . Note the AND
$que = $db->prepare("SELECT posts.*, users.*, followers.follow, followers.followed_by FROM posts
INNER JOIN users ON users.user_id = posts.added_by_user_id
LEFT JOIN followers ON ( followers.follow = posts.added_by_user_id
AND followers.followed_by = $id )
WHERE (posts.added_by_user_id = $id OR followers.followed_by = $id)
GROUP BY( posts.post_id )
ORDER BY post_id DESC");
$que->execute();
$posts = $que->fetchAll();
Anotber way using your code, just add DISTINCT after SELECT. I.e SELECT DISTINCT posts.* .....
Another way is using sub query
$que = $db->prepare("SELECT posts.*, users.* FROM posts
INNER JOIN users ON users.user_id = posts.added_by_user_id
WHERE posts.added_by_user_id= $id OR posts.added_by_user_id IN (SELECT follow FROM followers WHERE followed_by= $id)
ORDER BY post_id DESC");
$que->execute();
$posts = $que->fetchAll();
You can also use the below. The key function is GROUP BY
$que = $db->prepare("SELECT posts.*, users.*, followers.follow, followers.followed_by FROM posts
INNER JOIN users ON users.user_id = posts.added_by_user_id
LEFT JOIN followers ON followers.follow = posts.added_by_user_id
WHERE (posts.added_by_user_id = $id OR followers.followed_by = $id)
GROUP BY( posts.post_id )
ORDER BY post_id DESC");
$que->execute();
$posts = $que->fetchAll();
(I'm not too sure how efficient this is)

how to write a left join for make loop

I want to write this code in one query with left join statement. Currently I use php for loop this query but its not good and is too slow
It's app that have like and dislike option. I want number of every user's like
public function getUserLikedCount($id) {
$query = $this->db->query(
"SELECT * FROM `users`, post, `like`
WHERE users.id = post.post_user_id
AND like.like_post_id = post.post_id
AND users.id = ?
AND post.post_is_active = 1", array($id)
);
return $query->num_rows();
}
thanks
Try like it ( I don't know your table like structure):
$sql = 'SELECT u.id AS `user_id`
COUNT(p.post_id) AS `liked_cnt` <-- or like.id ( if this field exists)
FROM `post` AS p
LEFT JOIN `users` AS u
ON ( u.id = p.post_user_id )
LEFT JOIN `like` AS l
ON ( l.like_post_id = p.post_id )
WHERE p.post_is_active = 1
AND l.status = 1 <-- "liked"
GROUP BY u.id ';
Try this :
$query = $this->db->query(
"SELECT * FROM `users` u
LEFT JOIN post p ON u.id = p.post_user_id AND u.id =?
LEFT JOIN like l ON l.like_post_id = p.post_id AND p.post_is_active = 1
", array($id)

Php sql query working fine but as i use two condition in same join not working

Here is my query worked well but want to add another condition in it but it giving error Not unique table/alias: 'users'
Here is my code
$query = 'select group_concat(DISTINCT evaluation_section. section_name) as allowed_sections from evaluation_section
join section_permissions on (section_permissions.section_id = evaluation_section.section_id )
join users on ( users.user_id = section_permissions.user_id )
join users on ( users.user_id = section_permissions.assigned_for )
where users.user_id = '.$user['user_id'];
want select name from users table also and then use this condition join users on (users.user_id = section_permissions.assigned_for )
please help me
This is my edited text
this is my full working query
$q = 'select group_concat(DISTINCT evaluation_section. section_name) as allowed_sections from evaluation_section
join section_permissions on (section_permissions.section_id = evaluation_section.section_id )
join users on ( users.user_id = section_permissions.user_id )
where users.user_id = '.$user['user_id'];
but i want it to select name from users table and then match it with users.user_id = section_permissions.assigned_for so that it get the name of assigned_for id name
as before group_concat I use users.name then its working but it display the name of users.user_id = section_permissions.user_id but i want to display name of er_id )
where users.user_id = '.$user['user_id'];
but i want it to select name from users table and then match it with users.user_id = section_permissions.assigned_for
in same query because in two its not working
Try doing it this way:
$query = 'select group_concat(DISTINCT evaluation_section. section_name) as allowed_sections from evaluation_section
join section_permissions on (section_permissions.section_id = evaluation_section.section_id )
join users u1 on ( u1.user_id = section_permissions.user_id )
join users u2 on ( u2.user_id = section_permissions.assigned_for )
where u1.user_id = '.$user['user_id'];
Since you are joining "users" table twice, you will have to provide unique alias name to both of them.

Categories