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
19k 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
10k 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
8answers
4k 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
28k 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
285 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
692 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
6k 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 * ...
5
votes
1answer
503 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
6k 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
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 ...
4
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 ...
4
votes
1answer
157 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
309 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
198 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
361 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
238 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
314 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
212 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
250 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
555 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
2answers
468 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
6k 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
314 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
494 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
818 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
701 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
983 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
128 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
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
186 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
205 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
48 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
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
292 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
2answers
3k 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
363 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
3k 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
637 views

dbDelta only creates the last table

I'm using following install function with dbDelta as referring to the codex, articles on SO I watched out for the typical problems with e.g. one space instead of two spaced at the PRIMARY KEY ... ...
2
votes
2answers
1k 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
1answer
2k views

Can't use get_results() in ajax query

I'm using a jquery ajax call to get certain data from wpdb and I'm getting the following message: <b>Fatal error</b>: Call to a member function get_results() on a non-object in ...
2
votes
2answers
675 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
781 views

Using $wpdb object in a widget

I'm trying to make a very simple widget that access tables contained within the same database instance as my Wordpress database, but are not wordpress tables. I'm using the wpdb class according to the ...
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
2k 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 ...
2
votes
2answers
1k 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 ...