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

learn more… | top users | synonyms

0
votes
0answers
38 views

Loading posts from another wordpress database on the same server

I've searched quite a while and found several solution but apparently I'm doing a lot of stuff wrong. I'm having 4 different Wordpress sites and need to load the latest 2 posts of each site. For ...
0
votes
0answers
33 views

How to query for the first 5 elements published?

<div class="content left"> <div class="wrapper"> <div class="gallery-single-content left"> <div class="gallery-filter left" style="display: none;"> ...
0
votes
0answers
15 views

Object Cache - Avoid db queries totally

I am using object cache and have cached just about all queries but am stuck with the last 3, on a post archive page. They are all wp core queries. SELECT SQL_CALC_FOUND_ROWS wpm_posts.ID FROM ...
0
votes
0answers
124 views

Custom post type as HTML/PHP table

I have a custom page which displays some WP members (students) using $wpdb to query the database and display these members in a HTML table. This table gets generated (populated with records) when the ...
0
votes
1answer
51 views

duplicate entries in database while using save_post or wp_insert_post

Here is what I am trying to do - I have created a custom table in the database and I am trying to enter data into it everytime a post is created. The database-entry needs to happen ONCE AND ONLY WHEN ...
0
votes
1answer
102 views

Updating with $qpdb->query() always returns 0 rows affected

I'm experiencing an odd situation while running an UPDATE with $wpdb->query(). Given the users table, with columns user_id (Primary Key) and status, and the user 3 existing and with status 0, I ...
0
votes
2answers
55 views

Saving custom term value to the database in new table

So I'm trying to save the value of a custom term to my database. The table is created as is the custom term. The code below outputs the text field onto my custom terms page but it just doesn't save ...
0
votes
1answer
17 views

Quotes in table name

Since WP3.5, prepare() accepts placeholders as a security measure, instead of just appending the argument to the query. Therefore, $wpdb->prefix needs to become a second parameter, called by %s: ...
3
votes
3answers
7k 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
24 views

$wpdb returns duplicate posts

I have created a shortcode which shows the posts which were created after the user last logged in. The shortcode is working perfectly fine, but I am not sure why but I am getting 7 duplicate posts. So ...
4
votes
2answers
488 views

What is the advantage of the wp_options design pattern?

As we all know, a design pattern that is used frequently in Wordpress' DB code is the idea of persisting data for a particular object (blog/post/comment), indexed with a particular name or key. We ...
0
votes
1answer
85 views

WordPress get pagination on wpdb get_results

How do I gee the numbered pagination of custom wpdb result? below code will show one latest post from each authors in the site. I want to show 20 posts per page with a numbered pagination. $postids ...
1
vote
0answers
214 views

I am getting allowed memory exhausted in wp-db.php

Today I checked my error_log and found these errors in it. PHP Fatal error: Allowed memory size of 176160768 bytes exhausted (tried to allocate 5290845 bytes) in ...
7
votes
2answers
229 views

Is it mandatory to use $wpdb->prefix in custom tables

Sorry if this question is trivial. I'm just beginning to develop plugins in WordPress. In all the tutorials I found this: while creating the custom tables, $wpdb->prefix is used. Example: ...
1
vote
2answers
620 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 ...
0
votes
0answers
31 views

wpdb giving different results then query in SQL Editor

I'm trying to read candidates and the relation with expertises from 2 custom tables in my WP Database. This is my WPDB query : $candidates = $wpdb->get_results(" SELECT * FROM ...
0
votes
0answers
34 views

Run insert if no entry otherwise run update with ajax

Below function works with ajax ... but I am getting a null result ... I want that if there is no data in the database then insert operation is performed and when there is data the update should work. ...
1
vote
1answer
21 views

How can I migrate mysql_fetch_array to $wbpdb?

I am migrating a script with direct MySQL access to the $wpdb class, but it fails. Below is both original and new code next to each other. What is wrong? <!-- ...
0
votes
1answer
31 views

Why my query does not run with prepare

Hi Below is my query and it works with prepare keyword on the localhost but whenever I use prepare keyword with my queries on live server the data does not show up and as soon I remove prepare from ...
5
votes
1answer
1k views

get_results on large datasets

I am experimenting some very disturbing problem using a custom SQL query. I have a custom MySQL table called trading_datas having 165 lines for 4,4Mo. One field datas of my table is a big serialized ...
0
votes
1answer
70 views

$wpdb->prepare is not working like mysql_real_escape_string

I'm working on a WordPress plugin and recently I have planned to migrate from SQL to $wpdb. I'm using a method called $wpdb->prepare instead of mysql_real_escape_string but it does not work. Here ...
0
votes
0answers
80 views

$wpdb->get_results returns NULL

$qry = 'select * from wp_wcmanage_9999 where order_updated IS NULL'; $get_orders_data = $wpdb->get_results($qry, ARRAY_A); var_dump($get_orders_data);// Returns NULL When i am executing same ...
0
votes
1answer
17 views

How to set up prepared query using IN statement

I am a tad bit confused on how I would set up a SQL IN statement using a prepared query. Here is the code: $in = implode(',', array_fill(0, count($product_ids), '%d')); $results = ...
0
votes
1answer
135 views

wpdb and acf via wp rest api

I made a custom endpoint with wp rest api for /wp-json/dn/locations which in turn runs the following wpdb query $sql = "SELECT * FROM wp_posts p WHERE p.post_type = 'location' AND p.ID = 133;"; ...
0
votes
2answers
269 views

How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?

I have a custom post type named store and I have a custom taxonomy city, I also have a meta box with a field that is stored with update_post_meta( $post_id, 'store_postal_code', $postal_code ); it ...
0
votes
0answers
50 views

Inserting and updating rows with wpdb indreases integer fields by 1 point sometimes

OK, here is the situation: I have a table with the fields "ID" (int, auto incremented), "query" (text), and "counter" (int). I am trying to make it so if a selected value exists in the "query" column, ...
0
votes
1answer
186 views

$wpdb->get_results returns empty but value exists

this is my first question here. More then 5 years working with Wordpress, I always find the right way through the Google, but in this case I did not even know what to do, I really stacked :( So ... ...
0
votes
0answers
100 views

Combine WP_Query results with new WPDB->get_results Query

Is it possible to combine results from a WP_Query array with results from a new WPDB get_results() and use the loop to loop through the results? Essentially, I am trying to modify a shortcode that ...
0
votes
0answers
76 views

wpdb->query and non-ASCII characters in given query

recently I'm dealing with wordpress and I find myself running a table creation query in a plugin through the function $wpdb->query (this wpdb class). Everything is fine until I put a non-ASCII ...
0
votes
1answer
74 views

Custom database query to validate data

I cant find any syntax for custom queries, use of $wpdb is new to me so can anyone help me solving this?? if(isset($_POST['submit'])) { global $wpdb; $tablename='wp_form_subscribe'; $myrows = ...
0
votes
1answer
48 views

Read post meta values, only if posts are public

I want to retrieve meta_values applied to posts. Currently I execute the following query global $wpdb; $table = $wpdb->prefix.'postmeta'; $values = $wpdb->get_results( "SELECT meta_value ...
0
votes
0answers
164 views

Filter content by custom field and category

I'm trying to filter a Wordpress request with 2 parameters. By a custom field (REGION): There are 6 regions By category: There are n-categories with 1 value per post. As a result I would like to ...
0
votes
1answer
184 views

Get an array of meta_values for a user meta_key

I need to get an array of all the meta_values for a user meta_key. I found this code: $cities = $wpdb->get_col("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'my_cities_meta_key'" ); ...
0
votes
1answer
41 views

Function returning queried meta value based on current post ID

I have written several queries which sums download counts grouped by the post they are associated with, and other relations. They work great for exporting to CSV and other uses outside of WordPress, ...
0
votes
1answer
33 views

WPDB SQL Ignore `post_status` Parameter

I use this SQL statement: SELECT * FROM wp_posts LEFT JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id ) LEFT JOIN wp_term_taxonomy ON ( ...
1
vote
1answer
157 views

Get data from database using $WPDB

I'm missing something here. I simply want to get data from my database. So I tried this but keeps giving me an error. global $wpdb; $sql = "SELECT * FROM $wpdb->postmeta WHERE group='$group'"; ...
1
vote
1answer
715 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
25 views

Issue regarding $wpdb->prepare()

$wpdb->prepare() is adding single quote ('') like below. I am using below code $sql = $wpdb->prepare("SELECT * FROM %s ",$table_name); echo $sql; I am getting output like below SELECT * FROM ...
0
votes
1answer
22 views

How to add next height number in Order Attributes inside the Add new page. [duplicate]

By default, when a new page is added it will be given the next highest number not already assigned to a page.
6
votes
4answers
4k 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 ...
0
votes
0answers
107 views

sanitize_text_field and apostrophe problem

I've written a custom plugin to send data via a form to a personal database, different from the WP one. Everything is working fine except the fact that when you write a name, for example D'ALESSANDRO, ...
0
votes
2answers
61 views

Optimising amount of calls to custom fields

I have this code fragment in my codebase: foreach($this->adverisers as $adveriser) { $this->data[$adveriser->name]['image'] = bfi_thumb(get_post_meta($adveriser->ID, ...
0
votes
0answers
113 views

Save customizer default values to DB on theme activation

i am working with multisite. each time i activate some theme in new blog, i need to go to customizer and click the "save" to save all the values to db so i could use the with get_theme_mod if there ...
0
votes
1answer
102 views

How to get DB options format without saving? [closed]

Using Unyson framework. I am getting theme options form data saved to a file. Was able to do that via ajax and JSON.stringify(form values in json format) problem I have is that the form data ...
1
vote
1answer
40 views

WSoD being caused by this piece of code

This is a function I'm writing which is supposed to post an input along with some other information, to a database. As of right now, activating the plugin produces a WSoD, when the function is removed ...
1
vote
1answer
53 views

Avoiding “Usage of a direct database call is discouraged”

WordPress-Coding-Standards reports that "Usage of a direct database call is discouraged." It's complaining about my usage of $wpdb->insert() in a plugin. Is this not recommended? What is the ...
0
votes
0answers
109 views

API to push post data to WordPress and populate custom fields

I've been looking around for related posts and such in the codex and other Q&A sites for several hours now, but haven't really come up with much so I thought I'd ask all you fine people out there ...
0
votes
1answer
30 views

How to Generate a list of Most Commented post?

I'm trying to call a list of most commented post. But the call to the DB returns always the same post (the first one in the loop) This is my code in functions.php function ...
1
vote
0answers
41 views

Using $wpdb to fetch posts with meta data

I have a post type "shows" with 500 posts in it. I want to list all 400 in one page listing their title + 5 meta fields and a url to the show post thumbnail. I initially went the classic wordpress ...
0
votes
1answer
44 views

Plugin with connection to database - Single function

I'm creating a personal plugin with more files and I need to connect to an external database with WPDB. At the moment I'm repeating "new wpdb(...)" in every function, in every file, of my plugin. Is ...