queries inside of a class

class MyClass{ function __construct(){ global $wpdb; $this->db = $wpdb; } function query(){ return $this->db->query( $this->db->prepare(“INSERT INTO {$this->db->wp_competitors} (id, ield_key, field_value) VALUES ( %d, %s, %s )”, 1, $field_key, $field_value) ); } }

Use WP_Query with a custom SQL query

You won’t be able to translate a complicated SQL query directly to a WP_Query. What you can do is return Post IDs from the SQL and pass those IDs into the WP_Query args. Do note that this is highly inefficient and will likely create slow queries as post__in is not fast. You need to modify … Read more

How to close wpdb connection? [duplicate]

WP 4.5 had introduced wpdb->close() method to close connection in current instance. Old answer There is no explicit method. It stores link identifier in $wpdb->dbh field, but it is protected, so not convenient to run close on.

Increment value (value = value+1) of $wpdb->update

You don’t. Not with the WPDB update() function. The update function assumes that the values of the columns are strings. You can override that with the format parameter, but that only allows %d, %f, and %s (integer, float, string). It doesn’t allow operations on columns like total+1. You’d have to use the query() function directly … Read more

wpdb->insert not working

I got the same problem. Solution 1: Strip your data.. WordPress rejects the query if the length of value is greater than the field length defined on the database. On your insert query, the value for post_type field exceeded the 20 character limit. Solution 2: Use the $wpdb->query method

Alternative to mysql_real_escape_string

When working with database in WordPress you should never use the low lever mysql_* or mysqli_* functions. Always use $wpdb methods, in your case you should use prepare(): $query = $wpdb->prepare( “SELECT post_title from $wpdb->posts WHERE post_title LIKE %s”, “%” . $myTitle . “%” ); Moreover, once you are getting a single column, you have … Read more

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