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

learn more… | top users | synonyms

1
vote
3answers
494 views

Bootstrapping Wordpress MultiSite Outsite of Wordpress - No $wpdb

I'm working on a plugin for a system to use Wordpress as the authentication backend, so that I can use my existing Wordpress users in another application. The plugin works great when running in single ...
0
votes
0answers
20 views

How to execute a SQL-query which contains multiple queries using $wpdb->query?

I want to execute the following Query-String, which contains 2 separate queries using $wpdb->query: CREATE TABLE `foobar` ( `id` int(2) NOT NULL AUTO_INCREMENT, `foo` varchar(22) COLLATE ...
0
votes
1answer
11 views

New User ID not working in custom function with user_register hook

I'm adding data to a table whenever a new user registers on the site. add_action( 'user_register', 'auto_follow_admin', 10, 1 ); function auto_follow_admin() { global $wpdb; ...
1
vote
1answer
31 views

How would I format a query that depends on post parent taxonomy

I'm trying to do a custom query which is outside the scope of a single WP_Query, I think I should be using $wpdb but my SQL is a bit ropey. Also I only need the post IDs returned not post objects. ...
0
votes
0answers
6 views

Querying same column twice with wpdb (for postmeta values, specifically latitude and longitude)

I've seen similar questions around, but I just can't apply them to my case, I'm quite new at this, apologies. I'm trying to create a google map of my posts, using a latitude and longitude that are ...
3
votes
2answers
356 views

Detecting errors generated by $wpdb->get_results()

How do I detect errors when using $wpdb->get_results()? For example: $result = $wpdb->get_results("SELECT * FROM this is not a valid query"); The preceding code doesn't generate any exceptions ...
6
votes
3answers
46k 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'", ...
0
votes
1answer
1k views

Action hook “wp_insert_post” works but not for last imported post

I want to run a custom function whenever posts are imported to my custom post type. I am currently running this code - function on_post_import($post){ global $wpdb; $results = ...
0
votes
0answers
23 views

$wpdb not working in file of WordPress plugin

I am working in WordPress plugin. I create a custom form where user add values and then click on submit button. When user click on submit button its redirect to custom process file, where i write ...
0
votes
1answer
25 views

Run an update query in a function

I need to run the below in a function, it updates my database if certain conditions are met. I can't get it to work and I can only assume its because of the "SET @newnum" at the beginning. I've ...
0
votes
2answers
643 views

How to use ajax in wordpress plugin to get data from coustom DB?

i have created php script that work great as stand alone. now i need to create this script as wordpress plugin. so i have created custom table in wordpress DB. then i tried to make the ajax call. in ...
0
votes
1answer
1k views

Inserting data into custom tables

I am trying to create custom input tables to send data into custom tables on the wp database. I cannot get my script to do anything. The user will enter a type of liquor, like vodka or whiskey, ...
0
votes
1answer
13 views

MySQL Select within WP Page template

I have one WP page which contains a form that posts data to a table in the Wordpress database called "members", data such as first name, surname, email, role and bio. Table posts fine and data is ...
0
votes
1answer
20 views

$wpdb->get_results breaking page?

I'm trying to generate a list of years that have posts on a Wordpress page using a "php in posts" plugin that has a shortcode. but the code I'm using seems to break the page. What could be the issue ...
0
votes
1answer
33 views

Multiple wp_options tables to share content across installs

My application has >10 user_roles, each able to perform completely different tasks, provided with an custom backend and no access to wp-admin. The extra functionality each user_role gets to use is ...
0
votes
1answer
35 views

Custom select query two tables by a meta key

I'm using an event plugin which creates wp_ftcalendar_events table in database with start_datetime column. Here's image of whole table: I want to query my posts by start_datetime. I know that I ...
0
votes
0answers
17 views

Does WP query update accept variables being built into it?

Trying to figure out if this is a WP specific quirk. The var_dump code shows the correct string to be inserted but the last query dump at the end shows an incomplete query with no variable being ...
0
votes
0answers
4 views

$wpdb->get_var not matching after first loop through foreach

I'm trying to match my array with a entries in a table and retrieving the ID of the entry. So I have the following array called $features. Array ( [0] => Body Side Moldings: ...
1
vote
2answers
47 views

Getting users who registered 360 days from current date

As per the title, what I want to do is run a custom query that (using WP-Cron) will run once a day to check for users who registered exactly 360 days before the current date (don't need time, just ...
0
votes
1answer
42 views

How to insert a single row to WP database table?

Intro: I have a function that handles a contact form variables in my website. user enters name, email and text. function.php gets variables using javascript. I get more information on the user ...
3
votes
1answer
426 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 ( ! ...
4
votes
1answer
129 views

So much data in postmeta

Last year started using custom fields and in one year I have 230,540 rows in wp_postmeta table. This year will add more data and new fields and I don't know what to do, to create new table or continue ...
0
votes
1answer
28 views

How to fix this insert? Don't work for me!

I need to fix errors of this insert because this don't work in my script. I'm a beginner in WordPress SQL. Thanks $wpdb->insert('wp_cadastrados', array ( 'ID' => NULL, ...
0
votes
1answer
36 views

Why is this $wpdb query looping 5 times?

I want to only return one value for this query, it's in my functions.php file. The idea is to return a value that I need once post_meta has been updated. It's returning the same (correct) value 5 ...
0
votes
0answers
23 views

$wpdb how can i save my postmeta table before querying it

This function is in my functions.php file. The query returns the value I'm looking for via phpmyadmin, but only AFTER the function has been run in WordPress. add_action( 'transition_post_status', ...
0
votes
1answer
44 views

wpdb query not returning results containing single quotes

I'm using a simple $wpdb query that searches the database for all other posts with the same title as the current one and then displays a link to them on the current post. So if I go to a post titled ...
1
vote
2answers
390 views

Exclude subscriber users from user list

I want to exclude Subscribers from a user list. My code is: $users = $wpdb->get_results( "SELECT display_name FROM $wpdb->users ORDER BY display_name ASC" ); How can I change that so ...
1
vote
1answer
42 views

Convert simple SQL Query to WordPress query

I'm trying to 'batch' update the value in my post_parent fields in my wp_posts table. I can do it from within phpmyadmin using the following SQL Query: UPDATE wp_posts SET post_parent = '8' WHERE ...
0
votes
1answer
34 views

Problem in using wpdb

I used "wpdb" in my code But it was not working and the following error: Fatal error: Call to a member function insert() on a non-object Notice: Trying to get property of non-object" in ...
0
votes
0answers
18 views

How do I update post based on meta_key in another table?

So I have a function that is called by the wp_schedule_event (cron job) once daily. The goal of the function is to set all posts with a field wpcf-engine-days-to-go that has a value of "0" to a draft. ...
0
votes
1answer
19 views

wpdb insert working in one function, but not another

I have the following function: function addToShortlist($propref) { global $wpdb; if (isset($_SESSION['shortlist'])) { $shortlist = new Namespace\Shortlist($_SESSION['shortlist']); ...
1
vote
1answer
15 views

When and why should I use $wpdb different return types?

This one returns an object $mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10"); An associative array $mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE ...
0
votes
3answers
10k views

wpdb->prepare and mysql UPDATE - how is it done?

Trying to set a 'removed' date time in a plugin query but I'm not sure how to use UPDATE SET with $wpdb->prepare. Here's my query: $cur_date = date('Y-m-d G:i:s'); $rows_affected = ...
0
votes
1answer
75 views

Convert a WP Query into a simpler SQL query to fetch only COUNT of posts

I have this WP Query running for each row in my admin screen for a certain post type: $entry_args = array( 'meta_key' => 'wp_comp_entry_competition-id', ...
0
votes
0answers
52 views

Export Wordpress Table to CSV from page

I want to export my members table to csv format on my default download location. So I used a shortcode in functions.php and called it on a page. I am getting an already sent header warning and csv ...
1
vote
1answer
441 views

Wordpress choose wrong database

I have 2 databases. When I include the database connection (with a include('filename.php');) wordpress acts really strange. I can't login, the wordpress username and password is right but it tries to ...
-1
votes
2answers
2k views

Call to a member function get_results() on a non-object in wordpress plugin

I am using revolution slider plugin. I got above error while executing one of my front end form. This is my code in that file.. <?php class UniteDBRev{ private $wpdb; private $lastRowID; ...
0
votes
1answer
31 views

Site going down due to slow queries

My site keeps going down and the host says it's due to some slow queries overloading the server. They've supplied a list of slow queries, any pointers as to what I should be doing to get rid of or ...
0
votes
1answer
18 views

meta_value timestamp older than now

In my wp_posts table I want to select the rows where post_type == event. I also want to a connection with wp_postmeta from the specific row. In my wp_postmeta table I want to do a where like this: ...
1
vote
2answers
2k 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 ...
12
votes
2answers
10k 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 * ...
1
vote
1answer
47 views

Problem in inserting row to custom database table

I have a custom table in database named wp_tasks which has 5 fields. But I am unable to insert the current time to assign_date field. wp_tasks table id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, ...
0
votes
1answer
45 views

form $_post action value gets truncated after it passes through two forms

There are three pages page1, page2 and page3. When I press submit on my first form on page 1 my group name field can be catched with $_POST['groupname'] in the destination page but in the page 2 I ...
0
votes
0answers
26 views

Passed variable gets undefined variable error on insert on next page

I am passing a variable with $_POST to another page and on the destination page I want to perform an insert operation and want to use that passed value . The variable "$mygroup" is coming from a forms ...
1
vote
2answers
40 views

Database query works fine outside Wordpress

This is my code <?php global $wpdb; $rows = $wpdb->get_results("SELECT `submit_time` AS 'Submitted', max( if( `field_name` = 'Salutation', `field_value` , NULL ) ) AS 'Salutation', max( if( ...
0
votes
0answers
20 views

Get wp_dropdown_users to set variable in page

I have a page my-bookings.php which is generated by Events Manager Pro Plugin. Is shows all bookings made for the logged in user. Great overview, but the same overview is not available for Admins and ...
5
votes
2answers
2k views

How to programatically change username (user_login)?

As in the title, how to programatically change user's login? I wanted to use wp_insert_user function, but it appears that when updating current user, it doesn't change their username. Should I use ...
8
votes
9answers
7k 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 ...
0
votes
0answers
28 views

I am not getting result from php function that is json encoded

So I am trying to build an array of post data. But when I json encoded it I am not getting back the post object data from the database. Here are my two functions public function ...
0
votes
0answers
17 views

wpdb insert escaping

I have created a function that adds some data to a database table using wpdb->insert() Each time the form is submit it adds back slashes to single quote ' character, if form validation fails and ...