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

Delete all posts of a custom post type—efficiently

You can delete all post via $wpdb DELETE FROM wp_posts WHERE post_type='post_type'; DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts); DELETE FROM wp_term_relationships WHERE ...
pathusutariya's user avatar
23 votes

Wordpress Database lost auto increment

I had a similar issue, I solved it and since this comes up high on Google for what I was looking for it may help others. I migrated several Wordpress databases from AWS RDS MySQL to MySQL running on ...
Tim's user avatar
  • 335
18 votes

Delete all posts of a custom post type—efficiently

This can now be done with the WordPress CLI using wp post delete. Once the CLI is installed, the following terminal command (while in your site's root directory) will delete all posts of type ...
jwinn's user avatar
  • 281
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
16 votes

Get url of product's images (woocommerce)

Firstly, the GUID is not the URL. It is a "globally unique identifier". WordPress uses the URL for this purpose, but accessing it is not a reliable way to get the URL for anything (this was a bad idea,...
Jacob Peattie'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
Accepted

Please explain how Wordpress works with MySQL character set and collation at a low level

There are two defines in wp-config.php of WordPress website: define('DB_CHARSET', 'utf8'); define('DB_COLLATE', ''); There are several things which are most commonly misunderstood. Names of ...
Frank P. Walentynowicz's user avatar
14 votes
Accepted

Wordpress database error with latest WP - "WP_termmeta doesn't exist"

You are missing a table. You can add it using this sql CREATE TABLE `wp_termmeta` ( `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `term_id` bigint(20) unsigned NOT NULL DEFAULT '0', `...
Raymond Hon's user avatar
14 votes
Accepted

How do i get user data from a custom table in the wordpress database by user ID?

The first section of your code is correct global $wpdb; $table_name = $wpdb->prefix . "wplusersprofiles"; $user = $wpdb->get_results( "SELECT * FROM $table_name" ); The problem is in ...
Sourav Mukhopadhyay's user avatar
13 votes

Delete all posts of a custom post type—efficiently

You can delete all posts of a custom post type in various methods but here I am gonna show you how to do this without using SQL query. Here, for example, our post type is product $allposts= get_posts(...
Saruque Ahamed Mollick's user avatar
13 votes
Accepted

What is the database table for pages?

the wp_posts include all post types (post, page, custom post..), and to differentiate between them there is a field called post_type used to specify the name of the current entry whether it's a page, ...
Amine Faiz's user avatar
12 votes

what are pmxi tables?

This doesn't sound like something coming from WordPress core. Quick search online got pretty slim results, but it seems to be asscociated with WP All Import plugin.
Rarst's user avatar
  • 99.6k
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
11 votes

Where are wordpress custom types stored?

As mentioned by @milo in this answer Post Types aren't actually stored separately in the database however that being said... via SQL you can view all saved PUBLIC post types using the following sql ...
Craig Wayne's user avatar
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
11 votes
Accepted

MariaDB as a backend database

Yes, you can use MariaDB. MariaDB is a drop-in replacement for MYSQL so compatibility is not an issue. WordPress even lists MariaDB as an option within the requirements on their website: We ...
Marc's user avatar
  • 721
10 votes
Accepted

How to properly insert data into custom table via plugin

That's not a good practice trying to connect the DB by your own methods while WP does it for you initially. The problem with this is that I have to declare these variables ($db_host, $db_user, $...
Ismail's user avatar
  • 1,936
10 votes

How to Add an Index to Plugin Database table

Using dbDelta, on top of a PRIMARY KEY, you can include the word KEY to create an index for other columns: You must use the key word KEY rather than its synonym INDEX and you must include at least ...
froger.me's user avatar
  • 316
10 votes
Accepted

Fatal error: Call to undefined function dbDelta()

You answered your own question. You need to include the upgrade.php file which contains that function. Otherwise won't be loaded on your plugin update and the function won't exist. require_once(...
chifliiiii's user avatar
  • 2,842
9 votes

Post meta vs separate database tables

If your plugin is going to have A LOT of data, then using the wp_postmeta is NOT a good idea as demonstrated below: Taking WooCommerce as an example, in a store with ~30,000 products, there will be ...
unity100's user avatar
  • 291
9 votes

Wordpress Database lost auto increment

Why did this happen? Here's what went wrong for me: If you exported your database using phpadmin and had an error on reimporting it, the code that adds the primary key doesn't run because it's at the ...
Ben Erwin's user avatar
9 votes
Accepted

WP API V2 returning Invalid User ID

Wordfence blocks the User endpoint from the public. In settings there is a checkbox you can unselect to make it visible in the WP Rest API again.
erwstout's user avatar
  • 302
9 votes
Accepted

Where is custom template file chosen for a post stored in the DB?

It's in the post meta table (usually wp_postmeta), with the page id in post_id, meta_key with _wp_page_template and meta_value with the template file name.
Felipe Elia's user avatar
8 votes

Checking if Database Table exists

Try this one: global $wpdb; $table_name = $wpdb->base_prefix.'custom_prices'; $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ); if ( ! $wpdb->get_var( $...
Raximjon Komiljonov's user avatar
8 votes

#1062 - Duplicate entry '1' for key 'PRIMARY'

in the export process from your server's PhpMyAdmin select Custom options, then - be sure to tick under Object Creation Options the next option: Add DROP TABLE / VIEW / PROCEDURE / FUNCTION / EVENT ...
elicohenator's user avatar
8 votes

Restoring Wordpress posts from database only

Re-install a new copy of wordpress and then put in the credentials to your database in the new install. If you install wordpress and try to load the domain with no database attached, it will start ...
rudtek's user avatar
  • 6,121
8 votes

How do I check what plugins are enabled via the database?

You can do it like this: SQL SELECT * FROM wp_options WHERE option_name = 'active_plugins'; But for better approach will be the WordPress way: Wordpress if ( ! function_exists( 'get_plugins' ) ) {...
Drupalizeme's user avatar
  • 1,616
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

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