Questions tagged [wpdb]

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

Filter by
Sorted by
Tagged with
0
votes
1answer
10 views

Does WordPress have something like Drupal's DB API?

That is, the ability to write database queries like: $query = db_select('users', 'u'); $query->condition('u.uid', 0, '<>'); $query->execute(); I'm converting a Drupal site to WordPress, ...
0
votes
1answer
11 views

get posts whether meta_key exists or not

I am trying to get custom posts using $wpdb->get_results() as follows: SELECT p.ID, p.post_content, p.post_title, p.post_excerpt, pm1.meta_value as meal_date, (case when pm2.meta_key is null then ...
0
votes
0answers
8 views

XML post import from folder -> Checking title to avoid double import not working

I'm trying to import xml posts from a folder within WP. I'm using the following code: /** * Create and insert posts from XML files in folder */ add_action( 'admin_init', function() { ...
0
votes
0answers
6 views

How to query results for post_title and Custom Post Type/ACF field pivot query

I have a custom query that needs to match a search term to a custom post type of event by post title and by an ACF field host and then filters out events that have passed the current date to a REST ...
1
vote
0answers
14 views

Getting post revision and printing them on the post content site

I trying to print older post revisions on the content.php site (theme: twentytwenty), and this, with staticly set value works.. $query = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}posts ...
0
votes
0answers
47 views

How to insert a value to decimal type field using wpdb->prepare?

I have a table myTable with a decimal(12,2) field named balance. I want to update the table using the following code. $query = $wpdb->prepare("UPDATE myTable SET balance = %d WHERE id = %d&...
0
votes
1answer
22 views

WPDB: Update table

I am currently developing my first WordPress plugin and am currently a bit confused on how to change a record in a database. So far I have solved it using the $wpdb::update() function: public function ...
1
vote
1answer
16 views

How to get all term meta for a taxonomy - getting term_meta for taxonomy

TL;DR: I'm trying to get the meta_value display_type for a taxonomy, but it returns blank (even though I can see it in the database). This question wants to access a term meta that is on a the ...
0
votes
1answer
14 views

Save queried result into database

in multi author website when author publish post , he/her can give star rating to his/her own post. and I can display average rating for every taxonomy term using this formula: AVERAGE RATING=TOTAL ...
0
votes
1answer
31 views

Trying to get variable from WP table and toggle its value

I have a query setup in a function to grab the payment status from a table in wordpress using a search string. It will usually be 'Completed' (returned from paypal IPN). All i'm trying to do is allow ...
0
votes
0answers
16 views

Customising the default wordpress search functionality

I find the current from WordPress too strict as it doesn't allow for some minor spelling mistakes. How can I edit this function? I was checking out pre-get-posts however I don't think that allows me ...
0
votes
0answers
16 views

How to select custom posts thats are tagged with multiple taxonomies using wpdb

I want to select post id's of a custom post types (packages) that are tagged with multiple taxonomies (package_filters ['lights','sound','online']). The best I can do is this but its ugly. $sql = &...
0
votes
1answer
18 views

Custom search Dashboard widget

I have a custom dashboard that searches based on meta value. It works ok except that I now need to restrict the results/search to a specific post type, shop-subscription. This is the query I have, ...
0
votes
1answer
45 views

wpdb->get_row is selecting the variable as a column name

I'm using wpdb to get a row of a table $file = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}vip_files WHERE {$file_hash} = `hashcode`&...
0
votes
0answers
19 views

Does this archive template part look like its making too many calls to the db?

I set up a custom post type & taxonomy and registered it as a plugin. Then i'm referencing it from an archive template like this: <?php /* This loop is used in the Archive [.php] template. */ ...
0
votes
1answer
24 views

$wpdb->delete not working for me

I have a function (see in full below) that syncs data into WP from an external database. It's a list of scheduled items. When the schedule is updated, old data should be deleted. See this snippet: $...
0
votes
0answers
19 views

Replacing date values in TIMESTAMPDIFF() with custom fields from wp_postmeta

Two days ago I opened a thread related to the command 'timestampdiff()' -> Old thread). I think I've described the problem a litte bit too complicated, so I will describe it easier in this new ...
0
votes
0answers
26 views

$wpdb: using timestampdiff with custom fields

friends, sorry for my bad english. I hope, that someone could help me with my problem :-) I'm about to relaunch my website, which serves data of thousands of famous people. In the past i retrieved the ...
1
vote
1answer
35 views

Display custom fields from custom posts in RSS feed

I've been struggling with this for a few days and still can't figure it how. What I am trying to do is to add a custom table field into the RSS feed, so I am using Code Snippets for this matter. Below ...
1
vote
0answers
50 views

What is faster: custom taxonomy or serialized post-meta for db retrieval? (over 60,000 posts)

I have a search plugin that can search serialized metadata as well as categories (for multi-checkbox). Is there any advantage to using serialized post metadata instead of creating a custom taxonomy ...
0
votes
0answers
23 views

Is it possible to change the value in the database if it is less than 5

I have a custom table in my database named my-custom and value 19. Is it possible to return the value 19 when it is less than 5? I know I need to write the wpdb-> update function here, but I have ...
1
vote
2answers
31 views

Syntax for $wpdb->prepare when searching in two columns

I have this working statement for searching a single column in my database: $foods = $wpdb->get_results( $wpdb->prepare( "SELECT id, foodname, namevariations, calories, carbs, fat, protein, ...
0
votes
1answer
27 views

Set MySQL variables in WPDB

I have an SQL query that works beautifully as a Raw SQL query, however when I put it into WPDB it throws an error: global $wpdb; $sql = "SET @product_group = (SELECT product_group FROM ...
0
votes
0answers
23 views

Creating multiple tables with Plugin

I opened a new question, just to be sure. The former request has partly been resolved. I got another question regarding creating tables for wordpress: Only one table is created (The first one), but ...
1
vote
1answer
37 views

Problem with creating tables for Plugin

I hope someone can help me. I try to execute this code to install the tables for my Plugin. But for some reason it doesn't work (Doesn't appear in my wordpress database) and I can't find out why. Can ...
0
votes
0answers
17 views

any word search Wordpress

By default, WordPress searches by keyword. What this means is that when passed the search query football boots, WordPress builds the following for the WHERE clause: ((wp_posts.post_title LIKE '%...
2
votes
1answer
57 views

how to execute different sql query in non-sanitized $wpdb->get_results function

I saw $wpdb->get_results that function is not sanitized by core code($wpdb is get_results escaped). For example, in following code $wpdb->get_results( "DELETE FROM `wp_postmeta` WHERE `...
0
votes
0answers
8 views

page can't redirect in Faq custom plugin in admin page

I was trying to delete row in admin page for Faq custom plugin which I made. I made custom Faq plugin and connected it through database. Get all data on front-end and in admin page of plugin. I wanted ...
1
vote
1answer
18 views

Constructing a dynamic WPDB query with multiple LIKEs

I need to query the database table using multiple strings. A row should be selected only if the "tags" column matches all the strings. I don't know the amount of strings beforehand (it's ...
0
votes
0answers
26 views

Wordpress update databse table

Can anyone explain to me wpdb update database I would like to understand about $where (an array of field name). $table The table name. $data A PHP array where the keys are the columns and the values ...
0
votes
1answer
24 views

wpdb function for filtering the images by title

I want to write a function using wpdb by which if I click on an alphabet it will show images that starts with that alphabet
0
votes
1answer
30 views

Use $wpdb or other PHP script method to find/replace in WP database

I'm hoping to push a bug fix for a custom WordPress plugin my sites use. The plugin registers a shortcode with various attributes. However, there's a bug where one of the attributes is incorrectly ...
2
votes
1answer
165 views

Should close $wpdb via $wpdb->close()

I’ve been writing a few custom plugins for my site and I was wondering about the correct way to interact with $wpdb. My plugin executes a few queries using the global $wpdb connection. I noticed that ...
0
votes
0answers
12 views

What is the lowest hook deal with database?

I need to track every process done on the database, so what is the lowest hook applied to the database processes?
1
vote
0answers
14 views

How to Instantiate wpdb Object in New File

I have a shared hosting account which does not permit me to connect to my databases via mysqli or PDO. However, I can install WordPress and interact with the database via the various built-in forms. ...
1
vote
0answers
90 views

how to search users by ajax live search

in short i want to search users with with help of live ajax search . i have written a code and it is working fine but that code is for searching posts by ajax, but i want to search users from ...
0
votes
1answer
17 views

SQL queries to another wordpress site

I have 2 wordpress sites on the same hosting and the same host/user/ password for the database I have on the other hand 2 different database and 2 different prefixes I need, during an action on site1, ...
0
votes
0answers
26 views

when using $wpdb sudden connection rejection without changing anything. Access denied, with errors from repair tool

WP version 5.5.3 - updated days ago php version 7.3.24 running locally on server Can not create a backup using duplicator Occours bothi inside WP admin pages, so internal connections and external ...
0
votes
1answer
46 views

Insert NULL value using prepare()

I have similars queries /* Query */ global $wpdb; $tablename = $wpdb->prefix . 'data'; $sql = $wpdb->prepare( " UPDATE $tablename SET `date` = %s, WHERE id= %...
0
votes
0answers
20 views

Remove all \n\t from $wpdb->prepare()

If I have a prepare method with a lot of fields like the one below, to make the code more usable and clear, I always separate the rules of my query with newlines, but if I have problems with these ...
0
votes
1answer
31 views

wpdb COALESCE won't work

I am struggling to get the result using COALESCE from custom table. I need to run a second query if the first results null but wpdb resulting query string itself rather than the result. $query = "...
0
votes
1answer
81 views

How to get a value-only flat array from $wpdb->get_results when selecting a single column, without foreach()?

My query is: $var = $wpdb->get_results("SELECT field FROM {$wpdb->prefix}table", ARRAY_A); var_dump($var); it returns someting like array(2) { [0]=> array(1) { ["field"]=&...
0
votes
1answer
14 views

PHP | left join avg query give me Array()

I'm trying to implement a query within my code that gives me the ability to calculate the average value between the ratings received and the number of ratings per post, and sort posts from the one ...
0
votes
2answers
113 views

get_results query with accent

I'm trying to do an SQL query and I don't understand something. I get a value with $ _POST, this value is equal to 'définition'. I made this request: $sql = "SELECT DISTINCT * FROM". $ wpdb-&...
0
votes
1answer
17 views

Fill New Taxonomies

I will explain my scenary and then my problema.. I have more than 10.000 products on my woocommerce categorized by differents name. IE car parts. I need yo create a new type of taxonomy that I will ...
1
vote
0answers
73 views

How do you build a wpdb query dynamically?

My table has 6 columns, 3 of which I'm interested in: id, name, email. The problem is that my query does care about the name, but doesn't always care about email or id. As of now, the code is this: ...
0
votes
2answers
46 views

What is my error trying to create a new table in the database?

I tried creating the tables in different ways and I always get the same error: "Error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the ...
1
vote
1answer
47 views

Filtering posts from different categories into different section by doing WP_Query only once

I am trying to create a custom homepage with multiple categories section. I know this is possible with WP_Query, but i don't want to make multiple db trips. I want to query posts (say 5 each) from ...
0
votes
0answers
18 views

MYSQL TIMESTAMP when adding DATE_FORMAT then the output is blank, PHP conflict?

Usingthe following shortcode function, I have the code building a table by querying the DB. That all works fine. BUT when I add any type of formatting to the TimeStamp column such as DATE_FORMAT or ...
0
votes
0answers
97 views

Any way to use FETCH_KEY_PAIR with $wpdb?

I'm trying to get key/value pair results from a query like PDO's FETCH_KEY_PAIR from a $wpdb query. The closest I found was the OBJECT_K output from get_results(). But that returns the key, but for ...

1
2 3 4 5
23