Create an array with a string key from wpdb->get_results

Found the solution: $currentProducts = $wpdb->get_results(“SELECT feedid, id, size, price FROM products WHERE shopid = $shopid”, OBJECT_K); The OBJECT_K parameter makes an associative array with feedid as key: https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

Join inside a wpdb query.. confused!

$emailusers = $wpdb->get_results( “SELECT p.ID AS post_id, p.post-title, u.user_email ” . “FROM $wpdb->posts AS p ” . “INNER JOIN $wpdb->users AS u ON p.post_author = u.ID ” . “WHERE p.post_type=”sales” AND HOUR( TIMEDIFF( NOW( ) , p.post_date_gmt ) ) >=1)” ); foreach ($emailusers as $user) { // do wp_mail stuff from here // $emailusers->post_title; // … Read more

$wpdb->replace / Replace or update primary key

You Can use This solution.. How to delete all records from or empty a custom database table? In this solution First delete all records an then insert new records (your table should be have records before to delete query because If run $delete you give error ‘No find row’ ) and No need to use … Read more

How to email user after inserting the username in database in WordPress

Assuming that $member_details->user_login is already a user in the wp_users table, then you could use the following to get their email address: $user = get_user_by( $member_details->user_login, ‘login’ ); From there, you have the user’s email address and can use wp_mail() to email them: $subject = “My email subject”; $message = “My message body…”; wp_mail( $user->user_email, … Read more

XOR functionality for meta_query

Okay! So, having spent all the night in order to find a right solution… finally.. I did it. Here is the right method, that works in my situation. The $wpbd method is pretty cool and complicated at the same time. While using this method do not forget to cache queries properly with php-apc or memcache. … Read more

How to save html and text in the database?

Just use the wpdb insert and update API, no escaping or sanitizing needed as per the doc, just the raw data. Data: (array) Data to replace (in column => value pairs). Both $data columns and $data values should be “raw” (neither should be SQL escaped). Something like: $wpdb->insert( $wpdb->prefix . “myTable”, array( “doiBody” => $_POST[‘doi-body’] … Read more

Problem in creating table through plugin code

The above code should be placed in a function and then hook that with plugin activation hook? Here’s sample structure of plugin-name.php file. Note – Use CREATE TABLE IF NOT EXISTS instead only create table Code Updated on – 2012-08-04 <?php // this code goes into your my_plugin_name.php file function install_my_plugin() { global $wpdb; $table_name … Read more

How to update records using $wpdb?

Logic of my function was all wrong, should have sorted the result in $trans_array before the foreach loop. Here is the updated code that worked, if anyone needs it: function rkm_translation_update() { global $wpdb; $trans_row = $wpdb->get_row(“SELECT * FROM id_item_lid”, OBJECT, 0); $id = $trans_row->id; $item = $trans_row->item_id; $lid = $trans_row->lid; $trans_array = $wpdb->get_results(“SELECT * … Read more

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