Wpdb query for comment meta for current post

You can join to the comments table and use get_the_ID(): // set the meta_key to the appropriate custom field meta key $meta_key = ‘rating_total’; $ratingtotals = $wpdb->get_var( $wpdb->prepare( ” SELECT sum(cm.meta_value) FROM $wpdb->commentmeta AS cm JOIN $wpdb->comments AS c ON (c.comment_ID = cm.comment_id) WHERE cm.meta_key = %s AND c.comment_post_ID = %d “, $meta_key, get_the_ID() ) … Read more

I am not understandinhg $wpdb->prepare correctly

Ok, so there is one major problem with your code and it has nothing to do with escaping LIKE statements in SQL. But let me start from that… There is nothing wrong with your escaping. You should do it exactly like that: global $wpdb; // Create a SQL statement with placeholders for the string input. … Read more

$wpdb insert array

What is your table structure. Please be aware that a $wpdb->insert call translate the array to an sql statement of the form INSERT INTO table_name VALUES (value1, value2, value3,…) Since there is no such thing as an array type for colums in sql, you will need to either serialize any subarray or make a seperate … Read more

store custom WP table names in a global variable

The DB_ constant prefix in WordPress is generally considered reserved for DB_NAME, DB_HOST, DB_USER and DB_PASS. Using it for plugin-specific constants is, in my opinion, not a great idea. The only implication it might pose is if other plugins try to use the constants, but that’s purely theoretical. The proper way to do this is … Read more

How to get top 10 user is based on user meta value

I guess you can do with get_users, something like: $topusers = get_users( array ( ‘meta_key’ => ‘todayuserclicks’, ‘meta_value’ => ”, ‘meta_compare’ => ‘!=’, ‘orderby’ => ‘meta_value’, ‘order’ => ‘DESC’, ‘number’ => ’10’, ) );

Get random row from custom table

[Note: @marwyk87 posted his answer while I was composing this, which represents another way to fix the problem] You’ve got a simple syntax error in your SQL, because of the way you’re referencing the table name. You should just say $sql = $wpdb->prepare( ” SELECT * FROM {$wpdb->prefix}quotes ORDER BY RAND() LIMIT 1 “); and … Read more

$wpdb not returning data

Here’s your problem: if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $result is not a mysqli_result object, it’s literally the results of the query. Since you never passed a second parameter to the get_results method specifying what format you wanted them in, it’ll assume “OBJECT”, so the correct … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)