Getting a single MYSQL result where the field is an array - php

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 !

Related

How can I organize comments by like count in WordPress?

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.

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.

Multiple MySQL queries on one page (WordPress)

I am currently running the following query on my WordPress website:
PHP:
function get_details($key, $post_id) {
global $wpdb;
$values = $wpdb->get_results( "SELECT meta_value FROM {$wpdb->prefix}new_post_meta WHERE meta_key = '{$key}' AND post_id = '{$post_id}' LIMIT 1" );
return $values[0]->meta_value;
}
ON PAGE:
<?php echo get_details('facebook_url', $post_id); ?>
It is a new custom table which works in the same way as the WP posts meta.
This is run 20 + times on the page so I was wondering if there's a better way to do this?
Currently running the query direct in the database it takes 0.5128 sec and the database table is 36MB in size.
The query is really slowing the site down.
Thanks to shadow for stating the obvious but it worked:
public function get_meta_data() {
$meta_keys = array(
//META KEYS GO HERE IN ARRAY
);
$mks = implode("', '", $meta_keys);
//Get Results
global $wpdb;
$results = $wpdb->get_results( "SELECT meta_key, meta_value FROM {$wpdb->prefix}resorts_meta WHERE meta_key IN ('{$mks}') AND post_id = '{$this->id}'", OBJECT_K );
foreach($results as $key => $value) {
$this->$key = $value->meta_value;
}
return "SELECT meta_key, meta_value FROM {$wpdb->prefix}new_post_meta WHERE meta_key IN ('{$mks}') AND post_id = '{$this->id}'";
I then created a function to display the results in $this->variables like so:
public function get_meta( $key ) {
return $this->$key;
}
And then stored get_meta_data in the construct function.
Thanks Shadow.

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.

WordPress custom SQL

I have this function which contains some custom SQL:
function user_comment_count_by_meta( $user_id, $meta_key )
{
global $wpdb;
$count = 0;
$sql = "SELECT count(*) FROM $wpdb->comments comments INNER JOIN $wpdb->commentmeta meta ON comments.comment_ID = meta.comment_id WHERE comments.user_id = %d AND meta.meta_key = %s";
$count = $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $meta_key ) );
return $count;
}
What it should be doing is counting all the comments for a user that have a particular meta value attached to them and returning that number. So for example if a user has made 20 comments and then 11 of those have the meta value 'accepted' attached to them then the number returned would be 11.
I call the function like so:
<?php $count = user_comment_count_by_meta( get_the_author_meta('id'), 'accepted' ); ?>
However it doesn't return anything. Not sure where I have gone wrong? If any SQL geniuses could help or if anyone can spot a problem it'd be much appreciated. Thanks.
Well,I think that the SQL it's ok, but when you call your function you are using
get_the_author_meta('id')
and this function I think that have other meaning.
If you want the ID from the post author you must use:
get_the_author_ID()
I'm not sure. really.

Categories