How can I organize comments by like count in WordPress? - php

I am using the "Comments Like Dislike" Plugin in WordPress so my users can like other user's comments. This works fine but I am trying to create an addon plugin that will organize the comments by like count. I have been able to reorganize the comments in this way but the issue I am running in to is that users are no longer able to reply to comments because of this change. The like count is stored in wp_commentmeta like below.
comment_id
meta_key
meta_value
1
cld_like_count
3
My function selects and orders this table by meta value then retrieves the comment ID of each comment. It then uses this array of IDs to organize my comments.
<?php
function like_filter_comments( $comments ) {
return like_organize_comments( $comments );
//return $commentsbylike;
}
add_filter ('comments_array', 'like_filter_comments');
//organize comments so that most liked comments appear first
function like_organize_comments($unorganizedComments){
global $wpdb;
//$allComments = get_comments();
$user_id = 1;
$wpdb_prefix = $wpdb->prefix;
$wpdb_comment = $wpdb_prefix . 'comments';
$wpdb_commentmeta = $wpdb_prefix.'commentmeta';
$meta_commentlike = $wpdb->get_results("SELECT comment_id, meta_key, meta_value FROM $wpdb_commentmeta WHERE meta_key = 'cld_like_count' ORDER BY meta_value DESC;");
$i=0;
foreach($meta_commentlike as $key => $value){
//print_r( $key);
$metav = $value->meta_value;
$comment = $value->comment_id;
$arr[$i++] = $comment; //comment_id listed in order by likes
}
$commentlike = $wpdb->get_results("
SELECT *
FROM $wpdb_comment
WHERE comment_parent
IN (0)
;");
var_dump($commentlike);
return $arr;
}
The function above does successfully organize the comments by like count but it removes the ability to reply to comments. I made an attempt to only select comments with no parent but I am having difficulties because comment_parent is in the wp_comment table and the like count meta value is in the wp_commentmeta table. I've looked into joining the tables together but haven't found any similar columns to merge.

Related

Getting 'meta_value' when searching for matching 'post_id' and 'meta_key', 'meta_value' combo

postmeta table example
The end result is matching an array of 'post_id', sorted by the number of attendees. I know it is a WP backend but I have to do it in SQL, no 'get_posts' or anything WordPress related. What I'm wanting to do is confusing so I'll try to be clear.
What I Have To Begin:
$check_post_ids - the original array of post_ids I need to check
$start_date - the 'meta_value' each event's '_StartDate' needs to match.
What I Need To Do:
I need to check these three post_ids to see if they have a matching start date. If they do, I need to get an array of post_ids sorted from the highest to lowest number of attendees.
Currently I was planning on doing this with multiple SELECTs and foreach() statements, but I feel like there has to be a simpler way to do this ( i.e. One SELECT & foreach() ). Here's what I'm doing at the moment. I haven't finished it yet because I feel like there has to be a simpler way to do this. Newer to SQL and any help is tremendously appreciated!
$check_post_ids = array('484', '627', '982', '2435');
$start_date = '1963-10-20 19:30:00';
// iterate through array of post_ids and check if they have the same _StartDate
foreach($check_post_ids as $id){
$start_date_check = "SELECT * FROM `wp_postmeta` WHERE `post_id` =" . $id . " AND `meta_key` LIKE '_StartDate' AND `meta_value` = '" . $start_date . "'";
$start_date_check_result = mysqli_query($conn, $start_date_check);
// assign all post_ids with a matching _StartTime to a new array
if (mysqli_num_rows($start_date_check_result) > 0) {
while($row = mysqli_fetch_assoc($start_date_check_result)) {
$matching_post_ids[] = $row['post_id'];
// iterate through matching_post_ids array, get the _NumAttendees for each, and assign their id and _NumAttendees to an assoc array to be sorted
foreach($matching_post_ids as $id){
$attendees_check = "SELECT meta_value FROM wp_postmeta WHERE post_id = " . $id . " AND meta_key = '_ecp_custom_2'";
$attendees_check_result = mysqli_query($conn, $attendees_check);
if($upvotes_check > 0){
while($row = mysqli_fetch_assoc($attendees_check_result)){
$sort_by_attendees['id'] = $id;
$sort_by_attendees['attendees'] = $row['meta_value'];
$sort_by_attendees_array[] = $sort_by_attendees;
}
}
}
For the first part of the query, I think you can simplify your code by using SQL IN keyword. Basically it substitutes the role of your first array with all your post IDs. Then, you can write a SQL query like this :
SELECT meta_value
FROM wp_postmeta
WHERE meta_key = '_ecp_custom_2'
AND post_id IN (SELECT post_id
FROM wp_postmeta
WHERE post_id IN ('484', '627', '982', '2435')
AND meta_key LIKE '_StartDate'
AND meta_value = '" . $start_date . "'")
ORDER BY meta_value DESC
There are 2 queries. The first one in the parenthesis, subselect all the post ids on your table wp_postmeta where post_ids have ids in your list and if they match with your starting date. Then, the main query selects all meta_values (I suppose attendees) based on your first subquery (all post ids with your starting date).
Hope it will help you.

Getting a single MYSQL result where the field is an array

Wordpress database, bit stuck on this one.
I'm using the following to get the ID of the current user.
$user_ID = get_current_user_id();
This returns something like this :
15
Now I try to find the matching value of $user_ID in the field show_user_list The data in this field is stored in an array.
Which looks something like this :
a:2:{i:0;s:2:"29";i:1;s:2:"15";}
This is the query i'm running (along with a set of conditions) :
global $wpdb; $result = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE show_user_list IN (' . implode(',', $user_ID) . ' AND post_type = 'show' AND post_status = 'publish'" );
And then I'm trying to echo the value of the matching post_id with this :
foreach ( $result as $unique ) {
echo $unique->post_id;
}
But it's not returning anything. I know I must be making a mistake while dealing with the array but I don't know where I'm going wrong?
looks like you have stored a serialized array in the show_user_list field, so it will be a hustle to search for values into using a db query.
In the model you described, you have to select all the rows from wp_postmeta that match "post_type = 'show' AND post_status = 'publish'", then manually filter results that do not have the user id in the unserialized show_user_list field.
You might try something like :
in_array($user_ID, unserialize($row->show_user_list))
Also, I noticed multiple errors in your query: string not properly concatenated with PHP code and the right parenthesis of the IN clause not closed.
Regards,
same
EDIT
Here is how I would solve your problem providing info you have given :
$user_ID = get_current_user_id();
global $wpdb;
$results = $wpdb->get_results("SELECT post_id, show_user_list FROM wp_postmeta WHERE post_type = 'show' AND post_status = 'publish'");
$user_post_ids = array();
foreach ($results as $post) {
if (in_array($user_ID, unserialize($post->show_user_list))) {
$user_post_ids[] = $post->post_id;
}
}
Hope this helps !

get_posts() or similar from an array of post ids and display in loop

I have developed a custom search form for searching for a term in the 'postmeta' table and then returning an array of post id's that match that data.
I want to then get the WP post objects for each of the returned post id's in the array and run them through a loop to display the content on a custom page template.
I am able to do this with a PHP foreach loop and echo individual bits of data. Here is the function I have built to test the functionality I talking about.
function sales_search_data() {
global $wpdb;
$streets = $wpdb->get_results(
"
SELECT post_id
FROM $wpdb->postmeta
WHERE (meta_key = 'street' OR meta_key = 'suburb' OR meta_key = 'city' OR meta_key = 'zip')
AND meta_value = '" . mysql_escape_string($_POST['street_search']) . "'
");
foreach ( $streets as $street ) {
echo $street->post_id;
}
}
The $_POST['street_search'] is user input. And as I mentioned the query is working ok I just can't figure out how to get the Post Object into a loop from an array of Post Id's.
Thanks for your help.

How to retrieve all posts with all comments using 2 SQL queries

I am trying to make a page with a list of posts, and underneath each post all the comments belonging to that post. Initially I wanted to use just one query to retrieve all the posts + comments using the SQL's JOIN, but I find it impossible to for example retrieve a post with multiple comments. It only displays the posts with a maximum of 1 comment per post, or it just show a post multiple times depending on the amount of comments.
In this related question, somebody talked about using 2 queries:
How to print posts and comments with only one sql query
But how do I do this?
I've got the query and a while loop for posts, but I obviously don't want to run a query for comments for each post inside that loop.
$getPost = mysql_query('SELECT p.post_id,
p.user_id,
p.username,
p.content
FROM post p
ORDER BY p.post_id DESC');
while($row = mysql_fetch_array($getPost))
{
...
}
Table structure (reply is the table for storing comments):
POST (post_id (primary key), user_id, username, content, timestamp)
REPLY (reply_id (primary key), post_id, username, reply_content, timestamp)
You can do it in a single query, which is OK if the amount of data in your original posts is small:
$getPost = mysql_query('SELECT
p.*,
r.reply_id, r.username r_username, r.reply_content, r.timestamp r_timestamp
FROM post p
left join reply r
ORDER BY p.post_id DESC'
);
$posts = array();
$last_id = 0;
while($row = mysql_fetch_array($getPost))
{
if ($last_id != $row['post_id']) {
$posts[] = array(
'post_id' => $row['post_id'],
'user_id' => $row['user_id'],
'username' => $row['username'],
'content' => $row['content'],
'timestamp' => $row['timestamp'],
'comments' => array()
);
}
$posts[sizeof($posts) - 1]['comments'][] = array(
'reply_id' => $row['reply_id'],
'username' => $row['r_username'],
'reply_content' => $row['reply_content'],
'timestamp' = $row['r_timestamp']
);
}
Otherwise, break it into two queries like so:
$getPost = mysql_query('SELECT
p.*,
FROM post p
ORDER BY p.post_id DESC'
);
$rows = array();
$ids = array();
$index = array();
while($row = mysql_fetch_assoc($getPost)) {
$row['comments'] = array();
$rows[] = $row;
$ids[] = $row['post_id'];
$index[$row['post_id']] = sizeof($rows) - 1;
}
$getComments = mysql_query('select r.* from replies r where r.post_id in ("'
. join('","', $ids)
. '")');
while ($row = mysq_fetch_assoc($getComments)) {
$rows[$index[$row['post_id']]]['comments'][] = $row;
}
... Or something like that. Either option allows you to litter your first query with WHERE clauses and so forth to your heart's content. The advantage of the 2nd approach is that you don't re-transmit your original post data for each comment!
In order to also get those posts without comments, you need to use a LEFT OUTER JOIN. In that case, any row in the first table without any corresponding rows in the second table will be paired with a row consisting of null values.
SELECT * FROM posts
LEFT OUTER JOIN comments ON posts~post_id = comments~post_id;
By the way: there is also a RIGHT OUTER JOIN. When you would use that, you would get all comments, including those where the parent post got lost somehow, but no posts without comments.

Searching for unread posts in a database

Everytime a user reads a post, it assigns a cookie, eg.
set_cookies($id,'read',60*60*24);
But the problem is how do i select all the posts that hasn't been read by the user?
SELECT * from posts where (post is unread)
It doesn't require a login. Table structure:
ID | Content | Category
With your solution, you'd do something like this:
$ids = array();
if (isset($_COOKIES)) {
foreach ($_COOKIES as $cookie => $value) {
if (is_numeric($cookie) && $value == 'read') {
$ids[] = $cookie;
}
}
}
if (isset($ids[0])) {
$posts = implode(',',$ids);
$query = "SELECT * from posts where id in ({$posts})";
// Do the query
} else {
// no read posts.
}
But you should really look into storing your read variables differently.
I am assuming here that when user reads a post the id of the post read is stored somewhere. Let's for the moment assume that it is in the table read_posts that has a format:
UID | ID
In this case your query becomes:
SELECT * FROM posts WHERE ID NOT IN (SELECT id FROM read_posts WHERE uid = <user's id>);
If you only allow reading sequentially and store data in the same table the query becomes even simpler:
SELECT p.* FROM posts p, read_posts rp WHERE p.ID > rp.ID AND rp.UID = <user id>;
Syntax on this query might vary slightly but the general idea I think is clear.
If you can create a list of ids that have been read, yes:
SELECT *
FROM posts
WHERE ID NOT IN ($list_of_post_ids_that_have_been_read)

Categories