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

learn more… | top users | synonyms

3
votes
1answer
497 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( ...
17
votes
6answers
20k 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
0
votes
1answer
266 views

Stackoverflow type of badge plugin giving warnings in Wordpress 3.5

I modified an abandoned Stackoverflow form of badge plugin to work with Wordpress 3.4.2 and it does indeed. The full script for it is here: http://pastebin.com/Ta91zXiL When upgrading to Wordpress ...
1
vote
2answers
495 views

wpdb Custom Meta Data with 2 conditions

This is one of those problems that I've spent way too much time on, and there is probably a VERY simple solution that I am missing. Any help would be greatly appreciated. I am trying to populate a ...
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 ...
2
votes
1answer
294 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
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. ...
0
votes
1answer
146 views

Delete/replace img tags in post content for auto published posts [closed]

In my blog I have set up auto-publish for posts and I get 5 thousands new posts every day, in this moment I have more than 150 thousand posts. I need a script, or a plugin, or a function to connect ...
4
votes
1answer
243 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
310 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 ...
2
votes
1answer
266 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 ...
0
votes
3answers
2k views

Custom $wpdb Query for Custom Post Type by Category

I am using the following code to output a list of items in my 'documents' Custom Post type by Year and Month. <ul> <?php $post_type = 'documents'; global $wpdb; /**/ $years = ...
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 ...
3
votes
3answers
704 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 ...
1
vote
3answers
1k views

Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title

I'm working on a custom taxonomy term page template where we want the items that are connected to the term sorted by a publication date (custom date field) - and if there are multiple items on the ...
5
votes
2answers
695 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 ...
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 ...
-1
votes
2answers
65 views

Fixing plugin for wpdb::prepare

A previous developer created a plugin for a website I manage and everything seems to work okay, except I'm getting the error "Missing argument 2 for wpdb::prepare()". I understand that the arguments ...
3
votes
1answer
145 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
337 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
2answers
472 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 ...
2
votes
1answer
368 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
240 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 ...
1
vote
1answer
484 views

wpdb prepare syntax

$arr = []; array_push( $arr, 'wp_table', 1, '2013-12-24', 3, 'NULL'); $sql_prepare = "INSERT INTO %s (id, datea, one, two) VALUES (%d, %s, %d, %s) ON DUPLICATE KEY UPDATE one = VALUES(one), two = ...
1
vote
1answer
393 views

Merging PHP download script into `functions.php`

I run a large mixtape download site on WordPress. I'm currently developing a new theme and I'd like to include as much site functionality into the theme's functions.php. For serving the downloads, I ...
1
vote
0answers
466 views

$wpdb->get_results() returns good result only in first foreach iteration

I've got strange issue when I am using $wpdb->get_results() in content-single.php in Twenty Eleven theme. $wpdb->get_results() returns proper values only in first loop iteration. I've got simple ...
1
vote
1answer
297 views

How Can I Use $wpdb in PayPal IPN file?

I'm currently writing a plugin that requires the PayPal IPN, since the IPN is not inside the main plugin file, I have no way to use $wpdb. If you know a way to do this that will work with all ...
1
vote
1answer
3k views

$wpdb->insert - inserting multiple rows

How do I use $wpdb->insert to insert multiple rows. Here is my code. for($i=0; $i<=$urlCount; $i++) { $stat = $wpdb->insert( 'WP_URLS', array( 'POSTID' => ...
0
votes
2answers
69 views

Short of raw SQL, can I query for multiple attachment metadata that have a given array key?

With raw SQL through $wpdb, I believe that I could do something along the lines of this (untested): SELECT post_id, meta_value FROM postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value ...
0
votes
1answer
44 views

Create new database through static page code

I want to create a new table in current database of word press through create a static page. I write code for this as below, but there was no result. Why is that, and how can I get this right? ...
0
votes
1answer
304 views

$wpdb sql help. Select post id and post meta value based on 2 other post_meta values

I am trying to select post ids and a post meta value based on the value of 2 other post meta values and from a custom post type. Here is a screen shot of my post meta table. ...
0
votes
2answers
2k views

$wpdb returns no results with SELECT query on custom post type, works on default post type

EDIT: Adding more background info. I've coded a plugin that adds a meta box to the post page. When the post is published, the plugin will generate a random ID for the post, and insert the random ID ...
0
votes
0answers
96 views

get_query_var('paged') giving same result

I have a custom page, in that page i am using a custom select query for my search functionality. The problem is pagination not working. I used $on_page= get_query_var('paged'); for pagination. When ...
0
votes
2answers
145 views

Really simple query giving error in SQL syntax

I'm sure this is a simple oversight but I cannot seem to narrow it down: global $wpdb; $tablename = $wpdb->prefix . "icrm_folders"; $sql = $wpdb->prepare( "SELECT * FROM %s", $tablename ); ...
0
votes
2answers
300 views

Speed optimization of $wpdb->get_results

I'm using this code to generate all my custom field values from all posts for custom field named "autor". <?php $autor = $wpdb->get_results("SELECT meta_value AS autor FROM wp_posts, ...
0
votes
1answer
421 views

SQL query not working in alphabetical post title/content search

I have an ajax request using wp_ajax sending one variable named "q" what I want to do is after the script get's the request to look inside the db and fetch the title and id of the corresponding posts ...
0
votes
4answers
1k views

Trying to display posts by authors in with specific user meta

Ok here's the solution: <?php $wp_user_search1 = new WP_User_Query( array( 'meta_key' => 'state' , 'meta_value' => 'NM', )); $listers1 = $wp_user_search1->get_results(); $lister_ids1 = ...
0
votes
1answer
1k views

Wordpress $wpdb get posts from category and sort by custom meta

I have a custom post type of 'events' which has a meta key 'event_date' Im using the following to retrieve those posts and arrange them by 'event_date' global $wpdb; $today = ...
0
votes
2answers
1k views

How to change a custom query into a standard loop?

I'm trying to modify how results are displayed in a post list that's currently controlled by the code below. However, it's not built in the WP_Query while ( $the_query->have_posts() ) : ...