This means you’ll sometimes hear your site’s database referred to as a “MySQL database.” MySQL is what enables the database to store information and provide you with access to it. UNIQUE KEY id (id)
WHERE post_id = $post_id Out of the box, WordPress provides tons of functions that can be used to interact with the database. Question. AND meta_key = %s"// At this point, $mydb has access to the database and all methods"CREATE TABLE my_custom_table ( Create a Database enter the database name and click Next Step. We simply introduce the If we’re using the same username, password and database host, but only need to change the selected database, there’s a handy method called This is also particularly useful when we want to switch to another WordPress database, but still want to retain the functionality of functions like The WordPress default tables usually suffice to handle most complex operations. ; category_name (string) – use category slug. And sometimes, slow queries were actually fast queries at one point—but as the site grew older, the query got slower and slower, unable to keep up with the expanding database. ; category__in (array) – use category id. Sometimes, slow queries are a result of poorly-formed SQL that never should have been done that way. "INSERT INTO $wpdb->postmeta first_name varchar(55) NOT NULL, email varchar(55) NOT NULL, Your WordPress database stores every single one of your carefully …
WHERE post_id = %d In most cases, the In this tutorial, we’ll walk through the most important class to deal with databases in WordPress – Some developers make the general assumption that the table prefix will be unchanged and use default value of This is of course is an over simplification of what a plugin will be actually doing, but this example shows how quickly things can go wrong. Always Backup WordPress First! first_name varchar(55) NOT NULL, Each time you are viewing a WordPress page, there are MySQL queries running in the background to fetch the data from database. ( post_id, meta_key, meta_value ) ; category__not_in (array) – use category id. This can be easily fixed by replacing the The above code can be made portable by applying the changes as below:Even better, if you’re dealing with WordPress’ default tables, you can skip the prefix part and instead, directly address them as properties in And so on. Utilizing custom post types with post metadata, and custom taxonomies and term metadata, we can do almost anything without the need of using custom tables.However, custom tables might be useful whenever we want to have finer control over the data our plugin can handle. ) $charset_collate;"// Assuming we have our current database version in a global variable"CREATE TABLE my_custom_table ( However, when we need finer control of our data, it’s reassuring to know that WordPress itself provides various functions and classes like Firdaus Zahari is a web developer who comes all the way from Malaysia.
In order to do so, we simply add this line right after we create our table using the Using the same example as above, let’s say during development, we change our mind and we also want to store our customer phone number inside our table. However, for more extensive debugging and reporting, there’s always the Let’s take a look at how we can achieve the process of deleting Note that this is not the recommended way to delete a record in the database using However, this can be easily fixed! AND meta_key = $key""DELETE FROM $wpdb->postmeta Slow SQL queries can crush your WordPress site’s performance. UNIQUE KEY id (id) cat (int) – use category id. ) $charset_collate;" email varchar(55) NOT NULL, The above code can be improved further, since we’re querying the posts table by doing things this way instead:Apart from the unsafe nature of this snippet, it should run just fine with proper values. Make sure to use a strong password. It is based on the ezSQL class written by Justin Vincent, adapted to work with WordPress.The basic methods and properties of the wpdb class is already well explained on the WordPress Codex page so there’s no point to reiterate them here. phone varchar(32) DEFAULT '' NOT NULL, //new column In WordPress, queries are used to access data from your MySQL database.