The wpdb class, accessed via the $wpdb global variable, is used to interact with the WordPress database
14
votes
6answers
10k 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
6
votes
5answers
3k 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 ...
5
votes
2answers
202 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
7answers
1k 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
...
4
votes
3answers
12k 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'", ...
4
votes
1answer
1k 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
3k 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
122 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
189 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
96 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
127 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
66 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
175 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
132 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
1answer
245 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 ...
3
votes
2answers
451 views
Can i use php sql functions instead of $wpdb?
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 PHP sql ...
3
votes
1answer
583 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
1answer
363 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
1answer
842 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
159 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
1answer
619 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
1k 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
3k 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 ...
2
votes
3answers
3k 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
189 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
274 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 = ...
2
votes
2answers
2k 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
3answers
429 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 ...
2
votes
2answers
818 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. ...
2
votes
2answers
244 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
326 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
400 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
1answer
1k 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
67 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
700 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
359 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
352 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
60 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 ...
2
votes
1answer
245 views
Increment value (value = value+1) of $wpdb->update
This sql query
$wpdb->query("UPDATE log SET total=total+1 WHERE id='1')");
How do I turn it into $wpdb->update() statement?
Ex.
$wpdb->update('log', array('total'=>'total+1'), ...
2
votes
2answers
434 views
Query WP data with the WPDB API from outside WordPress
I'm working on a project with WP, a bit like a plugin, but not quite one.
I'm encountering an issue, when trying to include a file that contains some custom SQL functions in a class, in a completely ...
2
votes
1answer
163 views
Multipart/formatted MySQL query problem
I'm creating a plugin and I want to automate the creation of a MySQL Trigger on a table. The problem seems to be that $wpdb->query doesn't appear to accept either multipart statements and/or formatted ...
2
votes
1answer
227 views
Querying Email Addresses for a List of Users with Same Last Name?
I have a site with many users and many of them have the same last name. I want to get the emails of all the users with the same last name (IE: Smith) that has a post related to a particular taxonomy ...
2
votes
1answer
41 views
XOR functionality for meta_query
I'm trying to resolve the problem with event displaying.
An event has its start and end dates, written in meta. For example, event starts on (dmY) 03.04.2013 and ends up on 08.04.2013. Using WP_query ...
2
votes
0answers
89 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 ( ! ...
2
votes
2answers
110 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 ...
2
votes
1answer
51 views
How do I batch create revisions of all posts?
I cleaned up an old-site from pasted tags, manually created lists, etc, and I'd like to keep the old posts as "revisions", so we can easily "compare" with the old posts and retrieve/revert the ...
2
votes
0answers
193 views
post_content getting cut off on blank space on $wpdb->update
I am publishing the following post via xmlrpc
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><meta ...
1
vote
4answers
1k views
Export wordpress table to excel
I have created PHP scripts before to export a database table to .xls format like this:
$select = "SELECT * FROM tracking";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) ...
1
vote
3answers
1k views
Why would switch_to_blog stop working?
I'm using the Multisite functionality of WordPress 3.0, and I have a network of sites built where I display one random post from one of the subsites on the main site's front page. I accomplish this, ...
1
vote
3answers
154 views
Detect featured image among the attached images
I use the following code to extract the attached images from a post with ID:
$args = array(
'post_type' => 'attachment',
'post_parent' => $product_id,
...