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

learn more… | top users | synonyms

4
votes
1answer
174 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”. ...
0
votes
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' => ...
1
vote
1answer
117 views

MySQL Rank & $wpdb

I need a rank column in my MySQL statement, for example: SET @rank=0; SELECT @rank:=@rank +1 as rank, player, points FROM my_table Is there a way to use the above with $wpdb? WordPress doesn't ...
2
votes
2answers
899 views

Use $wpdb in wordpress theme files

I want to use $wpdb in a theme file, but it doesn't work. My code is bellow: <?php $action = $_POST['action']; $updateRecordsArray = $_POST['recordsArray']; if ($action == "updatelist") { ...
0
votes
2answers
893 views

How do I modify this wpdb query to include posts that have a post_status of publish and draft?

I'm using the following function to count the number of posts an author has made from a custom post type. <?php function count_user_posts_by_type($userid, $post_type) { global $wpdb; ...
1
vote
1answer
893 views

$wpdb doesn't appear to work on page inside of a plugin

I have a plugin folder foobar. Inside this plugin I have a page called foobar.php. Inside this page I have global $wpdb; $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : ...
4
votes
1answer
2k 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 ...
0
votes
1answer
858 views

AJAX wp-mysql running too slow

I have made a function of ajax, which returns all the posts starting with a particular alphabet, suppose a user clicks on letter A, then all the posts starting with letter A will be displayed, any how ...
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 = ...
1
vote
1answer
700 views

wp_create_user not properly entering password

I'm trying to create a new user after someone fills out a form. I'm using wp_create_user to do so. A user is being created properly, but, for whatever reason, no password is being submitted to the ...
0
votes
1answer
366 views

query for filtering published posts?

i need to filter count based on a date and post_status = 'publish', how to write a query to filter, as far as now i have filtered based on date, what is the query need to filter based published post. ...
0
votes
2answers
169 views

How to query and update one colum in postmeta table?

I'm trying to make a popularity calculation for all posts, based on page view and date of creation. Initially, there's a default popularity value saved in postmeta. Every hour I want to recalculate ...
0
votes
1answer
729 views

Formatting a date/time returned from a custom $wpdb query

I'm definitely a rookie WordPress/PHP developer, so please forgive ignorance :-) Q: How do I format a date/time returned from a custom query against $wpdb? Here's the scenario: I'm trying to write a ...
0
votes
2answers
14k 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 ...
0
votes
1answer
448 views

$post not working working in AJAX plugin with custom post type

I am trying to create a plugin that can relate two posts to each other, and to do so I am trying to use AJAX, but I am having trouble getting the current posts ID using $post->ID. I am trying to ...
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() ) : ...
6
votes
2answers
261 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 ...
2
votes
3answers
4k 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 ...
0
votes
1answer
663 views

Wpdb->insert() doesn't insert new row after the last one

In JavaScript, I'm using an Ajax call and I'm sending an array of shops. Except for the last element in the array all the shops already exist in the DB. The last element is a new shop. The PHP code ...
0
votes
1answer
1k views

Join inside a wpdb query.. confused!

I need to get a return from a query that provides me with email, username or nicename as well as post title based on author id. The query is to run once daily via cron and look for posts that have ...
3
votes
2answers
1k 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 ...
1
vote
2answers
241 views

How do I order my loop query?

I have a table in my database containing vote a tally I'm trying to order posts by. screenshot: the table I've tried something like this, which brings up all the voted posts and pagination okay but ...
0
votes
1answer
93 views

db query for retrieving comments without any reply

what is a query for selecting a list of comment id's that have not had a reply yet so I can use it to iterate though each comment id and echo out the post it is on and the excerpt of the comment along ...
0
votes
1answer
125 views

How to create a list of terms who's posts also have a predefined external term?

I have a pretty extensive music collection and my friends are always borrowing my stuff. I would like to keep better track of my library so I'm building a sudo-checkout system using WP. The ...
0
votes
1answer
550 views

Out of Memory - Line 791 of WP-DB.php (mysql_real_escape_string)

Update The end result is that I did not account for Apache memory limits. There were limits placed on the Apache process which caused PHP to be limited regardless of the settings placed on it. ...
2
votes
1answer
854 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 ...
1
vote
1answer
171 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
1answer
1k views

Can't use get_results() in ajax query

I'm using a jquery ajax call to get certain data from wpdb and I'm getting the following message: <b>Fatal error</b>: Call to a member function get_results() on a non-object in ...
0
votes
1answer
1k views

Query and get meta as object(stdClass) on wp postmeta table?

I want to get a groups of data from postmeta table. The postmeta table have 4 colums, meta_id, post_id, meta_key,meta_value. Each post_id has a list of meta_keys. I want to run a query and get a ...
0
votes
2answers
1k views

How to test the outcome of a wpdb query?

I want to get all the data with the meta key of "project_id" from post meta table. I wrote a query, it should work in thoery, but I have no way to see what I get, and inject this query to my plugin ...
-1
votes
1answer
296 views

How to get all the related ids without array?

I want to get all the post ids when the post meta includes a key of "project_id". This function get the job down, but need refine: function get_all_ids(){ global $wpdb, $post; $meta_key1 = ...
2
votes
1answer
372 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 ...
0
votes
1answer
5k 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
2answers
722 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 ...
7
votes
5answers
8k 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
413 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 ...
1
vote
1answer
390 views

Return XML of Post Metadata

All of my posts in one category (i.e. Location) have two custom fields: latitude and longitude. I would like to return an XML or JSON of all those posts (title, latitude, longitude) for use as Google ...
3
votes
1answer
2k 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
1answer
256 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 ...
3
votes
3answers
621 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 ...
6
votes
3answers
22k 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'", ...
3
votes
1answer
176 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 ...
0
votes
2answers
2k views

Use mysql_query instead of $wpdb query

I have a custom table inside my wordpress database called test. What if i query this table using mysql_query() instead of using for example $wpdb->get_results() Will this affect my performance? Ty ...
0
votes
1answer
204 views

Compare transient data with a meta box value

So I'm not sure what would be the best way to go about this in terms of best practices and optimization. Scenario: I have query that parses an external XML feed and stores the data using the ...
1
vote
2answers
2k views

mysql query two database tables, users and usermeta and sort by custom meta int value

I am trying to get an array of site users sorted by a custom meta value (an int). I can query the users just fine and get them back to use in a foreach. I currently have the query results come back ...
2
votes
2answers
635 views

Creating a Post form outside of the Admin

I am merging Wordpress into a existing system and require our users to be able to make posts to a multi-site WP install. I have a database table that will link our own member to specific blog IDs and ...
1
vote
4answers
2k 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
709 views

Using $wpdb object in a widget

I'm trying to make a very simple widget that access tables contained within the same database instance as my Wordpress database, but are not wordpress tables. I'm using the wpdb class according to the ...
4
votes
3answers
5k 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 ...
0
votes
2answers
646 views

Have working sql query… trying to adjust it to use $wpdb

So I have this code that queries a custom plugin generated table in the wp database. It works perfectly as is. But I'm trying to adjust it to use $wpdb instead of the $con function I have set up. ...