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

learn more… | top users | synonyms

1
vote
1answer
3k views

How does $wpdb handle COUNT(*)

I understand how to do simple queries and display results using $wpdb. This is my process: <?php $sql = 'select * from wp_votes;'; ?> <?php $votes = $wpdb->get_results($sql); ?> ...
1
vote
2answers
21k views

How to parse row results from $wpdb -> get_results

I have the following: $query = 'SELECT * FROM wp_pod_tbl_add_questions WHERE id LIKE '. $id; $row = $wpdb -> get_results($query); How do I get the ...
1
vote
1answer
2k views

Safe way to find last inserted id in a table?

Is it safe to use $wpdb->insert_id; to find the id of a last updated row id just after an update? ex: $sql = $wpdb->insert($table_name, $arrayWithDataToInsert, array('%s','%s')); ...
1
vote
2answers
23 views

wpdb custom post_type problem

I want to use custom post type in my custom SQL query, but, when I add the custom post type im my query, I get no results. Where's my probelm ? This is the code I've tried $top5_ov_performance = ...
1
vote
1answer
227 views

MySQL LIKE not working

It seems MySQL's LIKE operator behaves like a = operator. The following MySQL query returns the expected result (1 entry): $meta_key = '_locality'; $meta_value = 'The Hague'; $post_ids = ...
1
vote
1answer
83 views

Wordpress SQL query - returning 'true' 'false' or 'null'

I am working on coding a PHP form for my website that requires the user to enter a code before the form becomes active. I have a field called "used" that I set up as a Boolean/TinyInt which I thought ...
1
vote
1answer
154 views

Wordpress database error - Error in SQL syntax - I can't identify any error?

I am trying to write function that checks whether an email address is stored in my database or not. I've written the following PHP code:: function check_my_users( $email_address ) { global ...
1
vote
2answers
38 views

Get published posts and pages?

I'm working on a plugin to use gettext in posts and pages content. I'm trying to get all published posts and pages in order scan them for translatable strings, but I cannot figure out how to do it. ...
1
vote
1answer
90 views

Rolling Back from current WordPress version to previous one

What happens if we rollback from (for example) 3.8 to 3.5.1 and keep the same new DB created by the current version? Will that break anything?
1
vote
1answer
2k views

Delete row of custom table in WordPress using AJAX

From my custom table ($table = $wpdb->prefix . 'user_req';) I'm showing data with $wpdb->get_results() and a foreach loop: I managed to update the database with the individual values from the ...
1
vote
1answer
93 views

What's the purpose / logic of split_the_query in WP_Query->get_posts

In the get_posts method of WP_Query, there's a section relating to split_the_query The logic of split_the_query is $split_the_query = ( $old_request == $this->request && ...
1
vote
1answer
59 views

WPDB query - decrypting DB data

I am trying to grab data from the DB using WPDB but I am stuck on how to decrypt the data e.g. The data is stored as: a:2:{i:0;s:2:"92";i:1;s:2:"71";} I want the 92 & 71 as that's my post IDs, ...
1
vote
2answers
252 views

Why does $wpdb return strings for mysql integer values?

I find it very inconvenient and hard to understand that the default behavior of the Wordpress db-interface is to convert all integer (and float?) types to strings. e.g $wp_posts = ...
1
vote
4answers
9k views

get_results using wpdb

im trying to retrieve information on my wordpress database, i wanted to display all pages using this statement. but im getting a blank ARRAY global $wpdb; $result = $wpdb->get_results ( " ...
1
vote
2answers
51 views

Make one query for adding entries to database

I have a situation where I need several entries to be added to a database table (sometime a few hundred), but using $wpdb->insert() (and $wpdb->update() where edits are necessary) is running ...
1
vote
2answers
533 views

create drop down menu in theme customizer from custom db

what i am trying to do is create drop down menu in theme customizer from custom db i have created. db name "students". in the db i have only the id and the name. i want to show all students names in ...
1
vote
1answer
485 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
1answer
695 views

wp-admin post.php JavaScript Links Not Working

I just recently noticed in my backend admin panel none of the dynamic JavaScript links will open or work at all. This includes the 'Edit' links next to Status, Visibility, and Scheduled. Also the slug ...
1
vote
1answer
135 views

Modify the structure of data returned by $wpdb

Modify the structure of multidimensional array outputed by SQL via $wpdb The output that I need from $wpdb is easily achievable by running multiple queries within foreach. However, I am trying to ...
1
vote
1answer
42 views

Where to store the name of a custom table?

I'm extending the $wpdb object with properties for my custom tables. function setup_wpdb() { global $wpdb; // Create properties for custom tables $wpdb->my_plugin_table1 = ...
1
vote
1answer
2k views

ob_end_flush error when using wpdb in plugin

I created a small plugin to test database interactions in wordpress. I have a small function that inputs the arguments to the table. If i call my function in a themefile it inputs my test values to ...
1
vote
2answers
464 views

$wpdb: Counting posts corresponding to 3 terms in 3 different taxonomies

It's easier if I made an example: I have a custom post type MYPOSTTYPE, with 3 taxonomies TAX_1, TAX_2 and TAX_3 If I have 3 terms: 'TERM_TAX_1' in TAX_1, 'TERM_TAX_2' in TAX_2, and 'TERM_TAX_3' in ...
1
vote
2answers
1k 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'), ...
1
vote
2answers
326 views

Join new table with SQL query

I created a plugin with a new table (wp_soundcloud) and I need to retrieve the data from that new table along with the post data. Here is my query: $querystr = " SELECT $wpdb->posts.*, ...
1
vote
1answer
328 views

using custom taxonomies on non wp table?

I'm building a program registration system that will have a complex data structure. There are programs (custom post type, CPT), teachers (CPT), registrations (custom table and meta table), payments ...
1
vote
1answer
176 views

Most effective use of DB querys

I've got a custom database table with lots of data that I'd like to query several times from a custom template file I've built. Say the db table looks like this: id name city zip 23 ...
1
vote
2answers
813 views

Call External Object in Class Function During Callback

I've a class which has a function that check and creates table in database. In order to do so i need to use WordPress $wpdb object. I need the function to run only on first plugin activation, so i ...
1
vote
1answer
286 views

In what part of the Wordpress core does the users table and usermeta table get joined?

I have been trying to work out just where in the massive jungle of Wordpress include classes the usermeta table is joined onto the users table and if so, how does it work? The one confusing thing ...
1
vote
1answer
74 views

Can't get result from sql using ajax result

I'm using an AJAX form to feed the code something to lookup in db. Separate table, not WP options. I'm getting the result from AJAX, but when I try to do the sql it just returns 0. This is the ...
1
vote
1answer
150 views

wpdb insert and boolean fields

I'm preparing an insert query like this: $wpdb->insert('table', $data, $format); data array will be: $data = array_push($data, 'resp' => true) problem is, what $format shall i use for a ...
1
vote
1answer
51 views

Access Wordpress database outside of Wordpress

MAIN QUESTION: I am writing a wp plugin. Within it, I have a page located here: http://example.com/wordpress/wp-content/plugins/myplugin/page.php How do I access WP database from this page? Is ...
1
vote
1answer
36 views

I want to select the from values from database in WordPress? [closed]

Here is my code: $sql = 'SELECT * FROM ' . $wpdb->base_prefix . 'item WHERE uname = "'. $_POST['login_name'] . '" '; $result = $wpdb->get_results($sql) or die(mysql_error()); foreach($result as ...
1
vote
1answer
1k views

how to use $wpdb->prepare to update a custom table

As a noob I dont understand a lot of SQL injections but I need to be save so I have read that I have to use wpdb->prepare to make sure the data is stored correct. At this moment I use the ...
1
vote
1answer
499 views

wpdb update add current timestamp not working

So using the $wpdb->update to add some data to a custom table, trying to add the current timestamp but it's not saving the right stuff(0000-00-00 00:00:00 this is saved). overview code ...
1
vote
1answer
953 views

wpdb->insert multiple record at once

So after I have created the table 'settings-table' i want to add a couple of records to it. This is how I want to do it, not the best way, so I need a better way to do this because I will add more ...
1
vote
1answer
71 views

Confused by $wpdb->prepare

I am attempting to use $wpdb->prepare to protect against SQL injection attacks and am lost. I have the following snippet below which checks the h_transactions table for a transaction id. If it ...
1
vote
2answers
80 views

Optimizing Query used for a Shortcode

So, I have this shortcode that produces a list of all published multisite and their pages. The network in question has about 50 sites in it. The problem is that loading the page I put the shortcode on ...
1
vote
1answer
101 views

Search Everything & WP 3.7 update issues [closed]

I have been personally maintaining the Search Everything plugin for WordPress since the authors stopped. I upgraded one of my sites to wp 3.7 today but i get this error when i search: WordPress ...
1
vote
1answer
462 views

Error on inserting a form value to database

hi i have decided to store few values in a separate table, So i created it and now i used a form to store values but when i process that form i'm getting error Trying to get property of non-object and ...
1
vote
1answer
2k views

How to create database table, add data, update and delete using wpdb via plugins?

I am just starting out to create plugins with database. I know to create a custom database when activating the plugin. I just want to know whether we can create tables at the front end like adding the ...
1
vote
1answer
2k views

using $wpdb to get custom post type with term

I'm trying to get the title of the first custom post type with a specific term in a taxonomy. But I'm not great at SQL so therefor not great with using $wpdb. Here is my code: $posts = ...
1
vote
1answer
263 views

Correct way to perform non-cacheable DB query

Consider the code function getNow() { global $wpdb; $now = $wpdb->get_var("SELECT NOW()"); $wpdb->flush(); return $now; } I am using the above method to avoid caching ...
1
vote
2answers
138 views

wpdb->get_row() / get_results - is it possible to return an object instances of my own class?

Is it possible to make wpdb->get_row return an object of a my own custom class instead of stdClass?
1
vote
1answer
920 views

Select from wp_post and multiple meta_value from wp_postmeta

My lack of SQL skills has stopped me in my tracks here. I'm trying to select post_id, post_title and two postmeta values - the first has the meta_key of 'search_country' and the second a meta_key of ...
1
vote
1answer
827 views

$wpdb insert array

I need to insert multiple array. I get error: mysql_real_escape_string() expects parameter 1 to be string, array given My example code: $array_info is Array ( [0] => Array ( ...
1
vote
2answers
298 views

What is wrong with this wpdb update?

I've read a lot of documentation tonight, can't see why this isn't working. Obviously something I'm missing. $newstr = "18,19"; $defid = 1; $table = "wp_tablename"; $data = "array( 'somecol' => ...
1
vote
2answers
2k views

How to insert data to a database table when the field is not yet created?

Supposing I have an existing table(already created in dB) that handles user-registration (a plugin) and insert data using the $wpdB->insert like this: $success = $wpdb->insert( ...
1
vote
2answers
792 views

How to pass NULL in where array for $wpdb->update

how to get results of a row with NULL element? I have to update the row which contains only the post_id and the repeat value, 'start' and 'end' should be NULL mysql table screenshot: ...
1
vote
2answers
125 views

How do you use prepare when asking for a list of id's

I'm looking to get a number of records back from the database given a list of numbers and was wondering how I would use $wpdb->prepare to do this (to take advantage of the sanitising vs SQL injection ...
1
vote
1answer
351 views

Converting the_content string to an array?

I have created a custom taxonomy, this taxonomy is dedicated to image attachments, when I upload my images $post->post_content returns a string like: '[caption id="attachment_98" align="alignnone" ...