Skip to main content
Share Your Experience: Take the 2024 Developer Survey
34 votes
Accepted

How to use $wpdb to delete in a custom table

The best WP API solution for this goal is to use the delete() function to remove a row. A small example, to delete the raw ID in the custom table eLearning_progress. $id = 0815; $table = '...
bueltge's user avatar
  • 17.1k
17 votes

Get error messages when $wpdb->insert() returns false?

$wpdb->insert() method returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1). You can turn error echoing on and off with the ...
Krzysiek Dróżdż's user avatar
15 votes
Accepted

Wordpress Unit Testing - Cannot Create Tables

You've just discovered an important feature of the core test suite: it forces any tables created during the test to be temporary tables. If you look in the WP_UnitTestCase::setUp() method you'll see ...
J.D.'s user avatar
  • 3,915
15 votes

$wpdb->insert_id

There is no other way, but I can not see how you need any other way then this. I believe that the insert ID accessed by $wpdb is the last ID by this instance of WPDB, other inserts should not affect ...
Niels van Renselaar's user avatar
13 votes
Accepted

WPDB prepare - like % - placeholders?

Yes, they're normal. They were added in 4.8.3 to fix an SQL injection vulnerability. You can read an article describing the technical reasons for this happening here and the ticket for the change ...
Jacob Peattie's user avatar
12 votes
Accepted

$wpdb->delete column values IN ARRAY()?

No, wpdb::delete does not handle anything other than WHERE field = X. You can just use the query method instead: $ids = implode( ',', array_map( 'absint', $ids ) ); $wpdb->query( "DELETE FROM ...
TheDeadMedic's user avatar
  • 36.4k
12 votes
Accepted

How to call a PHP function with Ajax when the user clicks a button

Create a child theme so you don't mess with the code of an existing theme because next time you'll update the theme, you may loose all your changes (see Child Themes) Here is how to pass values to ...
Mike's user avatar
  • 576
11 votes
Accepted

How to fetch Data in WordPress using MySQLi or $wpdb

To fetch data from database table $results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results if(!empty($results)) ...
Rishabh's user avatar
  • 974
10 votes

WordPress 4.8.1 uses mysql_connect which doesn't work with PHP 7

This sounds like you do not have mysqli installed and/or enabled on your server. IIRC mysqli was added to php in version 5.5, and the older mysql extension had been deprecated and fully retired since ...
Mark Kaplun's user avatar
  • 23.4k
9 votes

dbDelta only creates the last table

For those trying to understand how some plugins, like WooCommerce, are able to do that without calling dbDelta for every table, the reason is the IF NOT EXISTS part of query. The dbDelta function ...
Felipe Elia's user avatar
8 votes

What's the proper way to use a custom table?

Should I be creating an interface for my new table? How do I go about that? Unfortunately, there is no interface to implement, you're going to have to build everything from scratch. This includes: ...
Tom J Nowell's user avatar
  • 59.4k
7 votes

WPDB Insert or if exists Update

You can try and update the table using $wpdb->update, I am using the id in the following code, but you can use any criteria. $result = $wpdb->update($tableName, $info, array('id' => $info[&...
Darrel K.'s user avatar
  • 171
7 votes

wpdb->prepare function remove single quote for %s in SQL statment

You can use %1s which excludes single quotes wrapper. By using %1s, you will note that the string won't be having single quotes and you will get the values directly as needed. $query = $wpdb->...
Malav Vasita's user avatar
7 votes
Accepted

wpdb get posts by taxonomy SQL

Sorry guys, I just found the solution: SELECT p.post_name, t.name as clientName FROM $wpdb->posts AS p INNER JOIN $wpdb->term_relationships AS tr ON ( p.ID = tr.object_id) INNER JOIN $wpdb->...
Web-Entwickler's user avatar
7 votes
Accepted

Updating all rows of table with $wpdb

It will not work as the update statement requires a selector to narrow down what to update. You should use a general query. For your needs use this: $wpdb->query( $wpdb->prepare( "...
Ovidiu's user avatar
  • 123
7 votes

Is there a (better) way to access $wpdb results?

The WPDB Class has quite a few methods which vary what will be returned. Using WPDB::get_results() returns an array of objects whose properties end up being what it expects to be returned. In this ...
Howdy_McGee's user avatar
  • 20.7k
7 votes

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 ...
Howdy_McGee's user avatar
  • 20.7k
6 votes
Accepted

How to execute mulitple statement SQL queries using $wpdb->query?

TL;DR This is not possible with wpdb. Use dbDelta(). Even better, use something else to run raw queries. Explanation wpdb->query() uses the mysql_query() function. From PHP manual: mysql_query() ...
XedinUnknown's user avatar
6 votes
Accepted

WPDB: how to get the value of a field in a custom database table

OK, so first of all, you should always use proper escaping, when building the queries. This is how your code can look after fixing: global $wpdb; $value = $wpdb->get_var( $wpdb->prepare( " ...
Krzysiek Dróżdż's user avatar
6 votes
Accepted

How to get all term meta for a taxonomy - getting term_meta for taxonomy

You're using $term->ID. Taxonomy terms do not have an ID property. They have a term_id property: $display_type = get_term_meta( $term->term_id ); You'll notice that WooCommerce is using this, ...
Jacob Peattie's user avatar
5 votes

wpdb->insert multiple record at once

$wpdb->insert function doesn't support multiple records to be inserted into single function call. You have to loop over your data and prepare the data to be inserted and then use $wpdb->insert into a ...
user54858's user avatar
5 votes

WordPress get pagination on wpdb get_results

You say "However, I know this is not a good way to do this" in your self answer. One thing I could add answering your question and using your answer is, you can use SQL_CALC_FOUND_ROWS with $wpdb $...
willrich33's user avatar
5 votes

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 ...
John Joseph Adigue's user avatar
5 votes

$wpdb->last_error doesn't show the query on error

If you want the query, that'll be $wpdb->last_query (note that you also do not need SAVEQUERIES, that's only if you want a log of every query ($wpdb->queries) last_error is... well, the error! ...
TheDeadMedic's user avatar
  • 36.4k
5 votes

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 ...
Louisa's user avatar
  • 205
5 votes
Accepted

Set media metadata (i.e. "dimensions" field) on SVG file after extracting it with a filter

The wp_update_attachment_metadata filter fires every time you upload an image through media library (specifically every time wp_update_attachment_metadata function is being called). So when an svg ...
Laxmana's user avatar
  • 1,332
5 votes

$wpdb->insert query changes to SHOW COLUMNS query Output

For anyone else finding this question. Wordpress seems to do a SHOW FULL COLUMNS query before executing the INSERT query. If it determines that the INSERT query would fail/store bad data, for example ...
Sumurai8's user avatar
  • 151
5 votes
Accepted

wpdb::prepare() isn't working

The issue is that in My/SQL table names are not strings. So 'wp_product_codes' is being interpreted as a string instead of as an actual table. This is why you usually don't see tables passed into wpdb:...
Howdy_McGee's user avatar
  • 20.7k
5 votes

Warning Notice $wpdb->prepare incorrect number of arguments passed

Numbered placeholders don't work as you'd expect, and are going to be removed altogether at some point in the future, so should be considered invalid syntax. So with this in mind, the error describes ...
Milo's user avatar
  • 78.5k
5 votes
Accepted

Is their any way to Extend WPDB class and Overwrite the Default Query Function

Calling wp_set_wpdb_vars() after replacing $wpdb will probably do the trick. $wpdb = new My_DB(); wp_set_wpdb_vars(); However, the only officially supported way to replace wpdb is to place a db.php ...
gmazzap's user avatar
  • 46.2k

Only top scored, non community-wiki answers of a minimum length are eligible