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
46 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 ...
2
votes
1answer
158 views

Is $wpdb->prepare escaping to much? How to use it properly?

Im pretty much confused by $wpdb->prepare. I want to use it to sanitize user input but it turned out that it destroys the query. I cannot imagine that this is how it's intended to work but I can't ...
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 ...
0
votes
0answers
72 views

There is way to improve this query? wordpress get post data and some meta data

Suppose we have 2 blog with different database A and B. I want to get some post from Blog B to show them on Blog A. The problem comes when the posts have much metadata (custom field for example) that ...
1
vote
1answer
237 views

wpdb->insert and stripslashes against sql injection

I've been reading the codex at this page: http://codex.wordpress.org/Function_Reference/stripslashes_deep The code i wrote before reading that page is this: $data = array( 'col1' => ...
0
votes
1answer
188 views

Use wpdb->prepare for `order by` column name

In my current plugin, I am picking up the order by column as user input. So I thought of using wpdb->prepare to escape it. $wpdb->get_results($wpdb->prepare("Select id from $wpdb->posts ...
0
votes
1answer
45 views

Is there a WP function to get taxonomy name from taxonomy_term_id?

I need to get the taxonomy name from the taxonomy_term_id. I am currently using the following to get it: global $wpdb; $term_taxonomy_table = $wpdb->prefix . "term_taxonomy"; $taxonomy = ...
1
vote
2answers
42 views

How to use IN array properly in Wordpress?

I have an array containting these values (including the apostrohpes): 'course_3202','course_3201','course_3200','course_3199' I try to fetch results from these values in postmeta-table. SELECT * ...
0
votes
0answers
49 views

Run query once inside a loop

I am trying to prevent to much queries inside a plugin one of my plugins. I have a query inside a function which will output a complete block with some layout elements and an input element which will ...
1
vote
1answer
19 views

get the number of queries made on a page(inside my plugin)

Is there an easy way to see how many queries you make on a page(inside my plugin)? As I dont want to many queries in my plugin.
0
votes
1answer
52 views

how to get db values without using an loop with wpdb->get_results()

So I want to return the data from 1 row from my custom DB table, but I dont want to use a loop as I only need the values once....so is there a way to do this? $results = ...
0
votes
0answers
57 views

Best way to execute custom page-specific mysql queries

I would like to query a secondary mysql database (not the Wordpress mysql database) with page-specific queries. I've seen many answers / tutorials on setting up a secondary mysql database, like: ...
0
votes
2answers
78 views

wpdb prepare: passing varible number of fields as second argument

As i want to manage the NULL fields in my db and wordpress functions doesn't allow to do so, i will need to dynamically generate a query depending on the situation. The problem is that i don't know ...
0
votes
2answers
606 views

Inserting data into `post meta` table?

How would I be able to insert data into the post meta table? I know how querying and wpdb works but I have no idea how I can just insert it into the table. I have these two fields I want to insert ID ...
1
vote
1answer
845 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 ...
0
votes
1answer
14 views

looking for a way to allow users to backup the plugin db data(save as)

I have a custom build plugin and I want to add the option to let the user save the plugin data(not using a thirdparty plugin to do this for me). So if a user clicks on the button it will allow them ...
1
vote
2answers
32 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
359 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 ...
0
votes
1answer
125 views

How to access the WordPress DB from a plugin file

I'm building a basic video management plugin, with an embed feature that will have URLs like: mysite.com/wp-content/plugins/embed-video.php?video=[POST ID] I plan on using the [POST ID] url parameter ...
1
vote
1answer
87 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?
0
votes
2answers
246 views

Undefined variable: row?

I'm trying to select the post_title from wp_posts with a query which I did using this method: global $wpdb; $test = $wpdb->get_var( $wpdb->prepare(" SELECT post_title FROM ...
0
votes
1answer
658 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 ...
0
votes
2answers
82 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
0answers
36 views

Proper way to trigger a MySQL query via link in a plugin

I am trying to develop a module in my WordPress plugin that will delete an entry from the database. The only issue is related to the trigger action. I've tried to send my users directly to the page ...
1
vote
1answer
26 views

Super administrator db name

As I cant test this(cant find any docs and dont have an multisite setup) I need to know the db name of the Super Administrator. So how is this role stored inside the wp db? It's probably something ...
0
votes
1answer
75 views

WP MS: How to query over the network

I have a WP 3.8.1 Multisite installation with 3 blogs, they're all in one db, prefixed wp_, wp_2_ and wp_3_. In a template in wp_ I try to to display all posts from wp_, wp_2_ and wp_3_ which have a ...
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 ...
0
votes
1answer
42 views

wp-postratings: list current user's unrated posts

This is the code I use to list current user's rated posts (I use the wp-postratings plugin on my site for rating posts): <?php global $wpdb, $current_user; $userranking = $wpdb->get_results( " ...
0
votes
1answer
123 views

List users with the most posts in the last 30 days

I'm trying to create a MySQL query to display a descending list of users with the most number of posts over the last 30 days. This is not working with me: $where .= " AND post_date > '" . ...
0
votes
0answers
71 views

Using the same shortcode to show any table from the database

I have a little question for you. I want to create a shortcode that makes a table with data that I have stored in a database. But i want to send the shortcode an attribute, to specify which table ...
0
votes
0answers
13 views

WPDB preparing delete from [duplicate]

I know my variables and functions are ok, it must be the way I have added the prepare that is stopping it working. function delete_record($table, $id) { //deletes a specific record global $wpdb; ...
0
votes
0answers
39 views

$wpdb: how to use a query as a base for another query?

I've got this query: select p.ID, p.post_title, p.post_content, t.term_id, tt.taxonomy, t.name, t.slug, tt.count from bdu_posts p inner join bdu_term_relationships rel on p.ID = rel.object_id inner ...
0
votes
4answers
139 views

Displaying post excerpt using wpdb query

I'm using a modified version of the code from a question (Get WordPress Posts Grouped by Month) to group my posts by year. Everything is working fine, but I'm having trouble getting the post excerpt ...
0
votes
1answer
100 views

Are term_taxonomy_id and term_id the same?

I have a custom query where I need a JOIN because I want to COUNT all the posts of a specific category. So I checked the tables and I found out that table wp_term_relationships contains a reference ...
0
votes
0answers
57 views

Update tags to 10,000+ posts with wpdb?

I have over 10,000 posts on a site which all need to have a tag added to them. WP_Query is either running out of memory or timing out on my server and in the past running large queries with this ...
1
vote
1answer
84 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
54 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, ...
0
votes
2answers
2k views

How to return number of found rows from SELECT query

I wrote a function which is supposed to return the number of rows found in a SELECT query but it always either seems to return 0 or an array. I have been messing around with this for about an hour now ...
0
votes
1answer
229 views

Add row to custom database Table and delete all rows older than 1 day

I'm editing a view counter plugin so that it logs the IP of every user that viewed a post. This is to prevent them from refreshing the page and thus creating artificial views. The part I'm having ...
0
votes
0answers
55 views

Order users by 2 custom fields in wpdb query

I've created a WordPress members site, each member is a user with the role of subsriber and has extra information set as custom fields - ie: address, contact details etc. I have the users output by ...
0
votes
1answer
76 views

wpdb select from using array as search parameters

Similar to how you can insert into an array using code such as $wpdb->insert( $table_name, $inputs); //where inputs is an array I want to do a sql select statement with code such as ...
0
votes
1answer
307 views

insert image alongwith with custom data via a frontend form

I use wordpress as a cms. I wanted to insert custom data via a form from the frontend in my wordpress site and then echo it on a page somewhere. After learning from here and there I am able to put ...
0
votes
1answer
42 views

$wpdb in php 5.5

I am helping a friend update their website and move it over to a new server. Their old server is running php 5.3.6-13 and the new site is running php 5.5. There is a custom plugin that uses $wpdb ...
0
votes
0answers
83 views

Querying the DB to get sum of column from custom meta box (only published posts)

I am trying to get the SUM total of a custom meta box, from all published posts, from a custom post type. By using $wpdb I am able to get the sum from all of the posts (published and otherwise), but ...
0
votes
0answers
2k views

AJAX with Selectbox Plugins (Select2, Chosen, Selectize)

I have been searching all over the internet for tutorials on implementing AJAX actions from jQuery selectbox plugins such as Select2 , Selectize and Chosen. (these plugins look better than the inbuilt ...
0
votes
1answer
200 views

Getting featured image with direct $wpdb within plugin

I'm using a customized version of the Last Viewed Posts plugin to grab recently viewed releases on my record label site. The code I'm using grabs an attachment image but it is not always the page ...
0
votes
0answers
36 views

How Can I Put Meta_Compare in the Database-Query?

Yes, I know that this can easily be done with built in wp_query but I need this in Wpdb query. I have a custom field and I need to display posts where this field does not exist. By the way can I get ...
0
votes
0answers
59 views

$wpdb->prepare - supporting multiple search terms

I am trying to write an escaped SQL statement, for example: $sql = 'SELECT * from TABLE where column LIKE %searchterm% AND column LIKE %searchterm2%'; Except I don't know how to handle it, as I do ...
1
vote
2answers
214 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
1answer
65 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 ...