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
14 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
9 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 ...
0
votes
0answers
22 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
13 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
1answer
42 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
1answer
30 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
13 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: ...
0
votes
1answer
11 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 ...
0
votes
2answers
36 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
50 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
101 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
181 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: ...
0
votes
0answers
24 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
21 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
13 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
25 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 ...
0
votes
1answer
40 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
43 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
9 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
2answers
80 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
1answer
72 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
0answers
37 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
91 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
57 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
56 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
35 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
46 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
1answer
79 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
34 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
25 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 ( ...
0
votes
0answers
97 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
24 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
21 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.
0
votes
0answers
64 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
52 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
88 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
85 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
39 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
44 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
84 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 ...
1
vote
1answer
93 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'"; ...
0
votes
1answer
25 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
31 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
34 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 ...
0
votes
2answers
94 views

Ajax insert or update data

I've been browsing through similar questions and have yet to get this functionality to work. I'm tracking the progress a user makes an embedded vimeo video and insert data or update data. JS on the ...
0
votes
0answers
35 views

$wpdb->insert running multiple times?

Im trying to run a $wpdb->insert ONCE on every page load (for testing purposes). The insert will insert a meta field for a post type. I have tried hooking the insert function into "init", add it to ...
0
votes
0answers
20 views

get_results is not working properly

I tried to find an answer in many forums for this problem, but it has been impossible. My problem is that when I do $wpdb->get_results in an empty table, mi webpage immediately stops working. ...
0
votes
1answer
34 views

Plugin will sort users by usermeta

As I refine my most popular plugin it will store a special number decoded from the user's birth date, which is an extended BuddyPress field in bp_xprofile_data. All users get a number between 1 and ...
5
votes
1answer
125 views

WordPress and multithreading

I'm working on a plugin. I've created an API that reads and writes to a custom table in the DB. This API can be called potentially from N clients simultaneously (and it really happens). I noticed that ...
0
votes
1answer
56 views

Alter the main search query to search posts by coauthor user name

I am using the Coauthors plugin: https://wordpress.org/plugins/co-authors-plus/ I have written some SQL in order to grab posts from authors whose display name matches the search query when searching ...