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

learn more… | top users | synonyms

20
votes
6answers
23k 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
12k 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 ...
7
votes
1answer
7k views

Should I use wpdb prepare?

I'm new to SQL and am wondering if I need to use wpdb->prepare for the following query to a table I've created global $wpdb; $tablename = $wpdb->prefix . "my_custom_table"; $sql = "SELECT * ...
7
votes
8answers
5k 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 ...
6
votes
3answers
33k 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
1answer
3k 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 ...
6
votes
2answers
302 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
3answers
8k 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 ...
5
votes
2answers
776 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
1answer
5k 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 ...
5
votes
1answer
555 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
2answers
590 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 ...
4
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 ...
4
votes
1answer
160 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
342 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
221 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
391 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
319 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
348 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
248 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
298 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 ...
4
votes
3answers
727 views

How might I retrieve a featured post image from an external WP site and display it as a link back?

I have two WP sites. I want to reach from one of them, into the other, and retrieve specific featured images for posts, and display them with a link to that post. I am using this line (with the ...
3
votes
3answers
2k 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
3answers
2k views

WPDB Insert or if exists Update

I'm not crazy familiar with WPDB or SQL in general but I have a custom table for my project and I'm trying to assign some metadata to it. What I'd "like" to happen is if a row exists, update it and if ...
3
votes
2answers
7k 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 ...
3
votes
2answers
100 views

Differences between wpdb->get_results() and wpdb->query()

Who can explain me the differences between wpdb->get_results() and wpdb->query() in making generic queries to wordpress db?
3
votes
2answers
660 views

$wpdb won't insert NULL into table column

When i try something like this $status = NULL; $wpdb->update( 'table', array( 'status' => $status, ), array( 'id' ...
3
votes
1answer
516 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
936 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
777 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
2answers
4k views

How to print the excuted sql right after its execution

I am searching for a way by which i can print the executed sql query just after the : $wpdb->query( $wpdb->prepare("INSERT INTO tbl_watchprosite ...
3
votes
1answer
82 views

WP_Query to work with custom view

I have created a Database VIEW on wp_posts which selects its all columns and exposes an extra calculated columns (average). So my View is identical to wp_posts plus one column, which I want to use for ...
3
votes
1answer
527 views

Is it true $wpdb->get_results is faster than WP_Query in most cases?

On my homepage, I have a section where I have to display the last five featured posts, where a featured post is simply a post with a custom field is_featured set to 1. I achieved what I want with two ...
3
votes
2answers
999 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
2answers
3k 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] ...
3
votes
1answer
191 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
267 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
2k 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
60 views

How to use mysql IN statement with wpdb update method?

I am using wpdb update method and wish to update records where the id is not found within multiple ids. So if I wanted to update records where each had an id of 1 I would do for example: ...
3
votes
1answer
145 views

Detecting errors generated by $wpdb->get_results()

How do I detect errors when using $wpdb->get_results()? For example: $result = $wpdb->get_results("SELECT * FROM this is not a valid query"); The preceding code doesn't generate any exceptions ...
3
votes
1answer
301 views

Change post status based on meta value

I have this cron set up to trash posts x days after it is posted. This works. Edit: Added my answer to my question. add_action( 'wp', 'do_trash_ads' ); function do_trash_ads() { if ( ! ...
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
6k 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
316 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
1answer
4k 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
2k views

How to use wildcards in $wpdb queries using $wpdb->get_results & $wpdb->prepare?

I am attempting to make a custom search for BuddyPress groups. Here is my method: public function search_groups($string){ global $wpdb; $results = $wpdb->get_results($wpdb->prepare( ...
2
votes
2answers
4k 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
1answer
459 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
5k 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

How to Get All Taxonomies AND All Terms For Each Taxonomy With Post Count Zero

Is there an easy way of obtaining every registered taxonomy and for each registered taxonomy, get all the terms for that taxonomy, and for each term, get the post count, without actually fetching all ...