The wpdb class, accessed via the $wpdb global variable, is used to interact with the WordPress database

learn more… | top users | synonyms

17
votes
6answers
16k views

Using wpdb to connect to a separate database

I want to connect wpdb to another database. How do I create the instance and pass it the database name/username/password? Thanks
7
votes
5answers
8k views

Showing errors with $wpdb update

I am using $wpdb->update to update a custom table I have in my database. When I var_dump the result it returns: int(0) So I tried $wpdb->print_error() to see what's wrong. However, it shows ...
6
votes
3answers
23k views

$wpdb->get_row() only returns a single row?

Why is it? I tried the same query in the console and it returned multiple rows. Here's the query: $this->wpdb->get_row("SELECT * FROM ".$this->wpdb->users." WHERE status = 'active'", ...
6
votes
2answers
261 views

Does wpdb add considerable overhead on queries with large result sets?

I am working on backup-related plugin and from some research of existing plugins - they seem to favor using PHP's mysql_* functions, rather than $wpdb. Is $wpdb overhead considerable enough that it ...
5
votes
2answers
612 views

Can i use php sql functions instead of $wpdb?

I'm new to WordPress and was wondering why in WordPress API do they tell you to use functions such as prepare(), insert(), get_col(), get_row(), query(), etc. when technically I can use the built-in ...
5
votes
8answers
3k views

what is the way to see the currently executing query in wordpress?

I am working on wordpress which is new to me Now I have been provided with site completely designed in wp But the problem is I am unable to search the query for each functionality/page I found ...
5
votes
1answer
443 views

Matching database content types to PHP types

The $wpdb query methods return all query results as strings. For example, an 'ID' column would return a string value like '42' instead of an integer 42. Is there a way to configure the WP database ...
4
votes
3answers
5k views

How do you properly prepare a %LIKE% SQL statement?

I'd like to use a LIKE %text% statement while still using the WordPress $wpdb class to sanitize and prepare input. SELECT column_1 from `prefix_my_table` WHERE column_2 LIKE '%something%'; I've ...
4
votes
1answer
2k views

wpdb->insert: do I need to prepare against SQL injection?

Do I need to use wpdb prepare before wpdb->insert? If I am inserting values into a wordpress table using wpdb->insert, do I need to "clean" my data before inserting it or does this method ...
4
votes
1answer
4k views

$wpdb->insert() and Values for Datetime Columns?

I have a custom table with a DATETIME column. I have a function to add records; I would like to add an $expires argument to this function which can hold a unix timestamp noting when the record should ...
4
votes
1answer
153 views

Theoretical Multi-Server WordPress Setup with Shared Users

The organization I work for would benefit if we could take the existing WordPress installations that are located on several (~5) different servers and be able to use a universal database with the ...
4
votes
1answer
275 views

Query causing load because of SQL_CALC_FOUND_ROWS post counting?

I found this query causing some load in the server, my hosting provdier asked me to look on it. SELECT SQL_CALC_FOUND_ROWS wp_posts I think this select no. rows found in wp_posts table.. but i ...
4
votes
1answer
167 views

Custom query to get post names beginning with a digit

I'm implementing an A-Z index for a custom post type. I've got my links printing out the page and the query working for the letters. However, the numbers are tripping me up because I don't want them ...
4
votes
1answer
308 views

What is the advantage of the wp_options design pattern?

As we all know, a design pattern that is used frequently in Wordpress' DB code is the idea of persisting data for a particular object (blog/post/comment), indexed with a particular name or key. We ...
4
votes
1answer
175 views

prepare() not working

When I do this: $transients = $wpdb->get_col( " SELECT option_name FROM $wpdb->options WHERE option_name LIKE '_transient_wb_tt_%' " ); It works ...
4
votes
1answer
261 views

find a random blogid across my multisite network that has at least one post published

I need to find a random blog across my multisite network that has at least one post published of a certain post_type, and return its blogid as a variable so I can use it in a switch_to_blog() call. I ...
4
votes
1answer
174 views

Get total number of comments from posts in a specific custom taxonomy

I’m having some difficulties trying to get total number of comments from posts within a specific custom taxonomy. The setup is as such: I’ve created a custom taxonomy for my posts, called “features”. ...
4
votes
0answers
200 views

WordPress database error: [Query was empty] [closed]

I'm using $wpdb to connect to a different database than my WordPress one like this: $newdb = new wpdb(DB_NEW_USER, DB_NEW_PASSWORD, DB_NEW_NAME, DB_NEW_HOST); I need to insert multiple rows into ...
3
votes
2answers
1k views

SELECT max(meta_value) FROM wp_postmeta WHERE meta_key='price'… stops working when value is over 999

Overview: I am not trying to return a post. I simply want the single highest value for a particular meta_value across all posts... just the value itself. Details: I have added a custom meta_key ...
3
votes
2answers
350 views

Does dbDelta delete columns as well?

I created a table using the dbDelta function and then removed a few columns and added a few. The new columns did get created but the old columns did not get deleted from the table. Is there a ...
3
votes
2answers
703 views

Display data from a non wordpress database on a page template

I am using a sql query to access information to display on a wordpress page by adding the below code to a page template. I know the query works as I've tested it in phpmyadmin. $rows = ...
3
votes
3answers
623 views

LEFT JOIN, INNER OUTER JOIN, LEFT OUTER JOIN is driving me crazy. Please help?

fellow coders! I an having a hard time JOIN(ing) tables. I have a a lot of users on my site and many of them have the same last names. I am trying to get their emails by their last name. WP stores ...
3
votes
1answer
469 views

$wpdb->prepare() warning in WordPress 3.5

I am receiving this warning when I try to run a database query: Warning: Missing argument 2 for wpdb::prepare() The offending query is: $result = $wpdb->get_var( $wpdb->prepare( ...
3
votes
2answers
947 views

$wpdb->insert is changing a value

I'm writing a plugin called 'Featured Releases' for a client. The user fills out a short form in the admin area which inserts some text and an image id into the wordpress database via $wpdb->insert. ...
3
votes
1answer
1k views

what are the differences between wp_users and wp_usermeta tables?

what are the differences between wp_users and wp_usermeta tables? i use wp-email-login plugin and i just noticed that if a user change his email in the profile page it saved in the wp_usermeta table ...
3
votes
1answer
176 views

How to make an activities stream mixing posts and comments?

What I'm currently trying to make is an activities stream wich list : The posts, the comments, both mixed between them and ordered by date. My idea is to make two queries, but i don't know how to ...
3
votes
2answers
173 views

Search multiple meta keys at once

How would I modify the following SQL Query to allow me to search multiple meta keys at once? e.g. user_business AND user_about In the end I'll be searching about 40 - 50 different meta keys; or if ...
3
votes
2answers
1k views

Does $wpdb->prepare not create a prepared statement that I can execute multiple times?

I am wondering from the syntax $sql = $wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] ) It appears (from the syntax - values within prepare) that it simple escapes values not ...
3
votes
1answer
2k views

$wpdb->query() multiple query support

Just bump into the issue that I can't execute multiple sql queries using the $wpdb->query(). Generated queries work fine directly in phpmyadmin so its something will the ezSQL class only. By design ...
2
votes
3answers
5k views

Pagination with custom SQL query

I have my own SQL string to select Custom Post Type posts with specific WHERE clause. I've used the offset and limit to return appropriate posts depending on a page being displayed. That's working ...
2
votes
1answer
257 views

Get terms that contain posts that in turn belong to other terms?

So I have custom taxonomy called "shape" and another one called "color". If I want to get the list terms under shape that contain posts that are also present in terms under color, how do I do that? ...
2
votes
2answers
4k views

$wpdb->insert not working in any way

I know this type of question has been asked over and over, but I couldn't find a solution for my problem, so I hope you can help me. I am using WP 3.3. and I have created a custom table. Now I want to ...
2
votes
1answer
3k views

Check for success of $wpdb->update() correctly

My code is if( $wpdb->update($tableName,array('opt_value'=>$cInfo),array('opt_name'=>'showWeatherWidget'))) //show success message else // show failure message This is not ...
2
votes
1answer
264 views

How can I connect to another WP database and use WP_Query?

I can connect to the other database with $db2 = new wpdb( $user, $pass, $db, $host );, but how to I get WP_Query() to use $db2? I want to be able to use the loop just the same with $db2 as I can with ...
2
votes
1answer
2k views

Fetch array with $wpdb

I am trying to convert this code to use the $wpdb. $data = array(); $query = "SELECT * FROM videos"; $query_exec = mysql_query($query) or die(); while($row = mysql_fetch_array($query_exec)) { ...
2
votes
1answer
2k views

WordPress Paginate $wpdb->get_results

Due to a complex multisite config, I have a query which combines the posts from two blogs and I would like to paginate the results. I am grateful for any help. I have posted my query. ...
2
votes
2answers
906 views

Use $wpdb in wordpress theme files

I want to use $wpdb in a theme file, but it doesn't work. My code is bellow: <?php $action = $_POST['action']; $updateRecordsArray = $_POST['recordsArray']; if ($action == "updatelist") { ...
2
votes
2answers
636 views

Creating a Post form outside of the Admin

I am merging Wordpress into a existing system and require our users to be able to make posts to a multi-site WP install. I have a database table that will link our own member to specific blog IDs and ...
2
votes
3answers
1k views

Does a query executed through wpdb class get cached?

I am not sure how WordPress cache the queries. I had the impression that whenever I execute a query through wpdb class, it gets cached. For example, on codex under Select a Row and Select a Var, it ...
2
votes
2answers
759 views

Creating a Front-end based User Search

I am working on a membership site which requires a front-end user search. I have been searching for an answer like a mad man, and I could really use some help! While there are several plugins that ...
2
votes
2answers
396 views

Database slowdown after update to 3.4.1

I own a wordpress installation, and Updated to 3.4.1. After the upgrade, I got a very strange slowdown to my wordpress. After all, I used the define('SAVEQUERIES', true); in my wp-config.php with ...
2
votes
1answer
719 views

get_results on large datasets

I am experimenting some very disturbing problem using a custom SQL query. I have a custom MySQL table called trading_datas having 165 lines for 4,4Mo. One field datas of my table is a big serialized ...
2
votes
2answers
2k views

Retrieving custom fields with $wpdb->get_results

I'm trying to use $wpdb->get_results to retrieve an array of all posts including custom fields. I can retrieve the meta_key and meta_value columns like this: [meta_key] => state [meta_value] ...
2
votes
1answer
72 views

How wordpress accesses its own database

Hi I'm just curious as to how Wordpress accesses it's own database, meaning that when I look into an admin page like link-manager.php or class-wp-list-table.php I don't see any occurences of $wpdb. ...
2
votes
1answer
857 views

How to filter a dd/mm/yyyy date from a custom field in a query

I'm trying to build a query in order to sort some posts by a date custom field, where the dates are formatted dd/mm/yyyy. It works by querying the database directly with get_results like in this ...
2
votes
1answer
372 views

get posts and postmetas in assoc array

how do I get all posts and postmetas in an associative array? $allposts = $wpdb->get_results(" SELECT $wpdb->posts.post_title, $wpdb->postmeta.meta_key FROM $wpdb->posts JOIN ...
2
votes
1answer
413 views

How to add/change a value of $wpdb property/var?

There is a problem: I want to add a table 'linkmeta' to work like 'postmeta' or 'commentmeta'.. and use functions like add_metadata / update_metadata / delete_metadata! but, in this functions call ...
2
votes
1answer
23 views

Using $wpdb Class to Pull Recent Comments Across a Network

Been looking for a clean and simple way to produce comments from across a network using the $wpdb database class. I did find this answer, which has been protected, and is four years old. I wrapped ...
2
votes
1answer
312 views

How to programatically change username (user_login)?

As in the title, how to programatically change user's login? I wanted to use wp_inser_user function, but it appears that when updating current user, it doesn't change his/hers username. Should I use ...
2
votes
1answer
290 views

How to order by blog ID in this multisite 'List Blogs' custom function

I found this really cool function to list all the blogs on a multisite network install. Because the original multisite list blogs function was depreciated :/ So I am using this function below ...