2022 Developer Survey is open! Take survey.

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
0 answers
13 views

WordPress get last inserted user id

I want to disable user_login field into my CMS. They are register only their email adress. I think to create secret user login by programmatically. I think for accessibility and manageability also I ...
user avatar
  • 258
0 votes
2 answers
20 views

Cannot get wpdb data (Error in a simple fuction) [closed]

I'm using Cryto Plugin and want to get coin price data from plugin db. For easy practice, I'm trying to get BTC data. <?php function get_coin_price($coin_symbol) { global $wpdb; $coin_price = ...
user avatar
0 votes
0 answers
17 views

Can't find out why dbDelta dosen't create my table

I have a function which needs to create a new table an d i can't see for the life of me why it dosen't create the table: function afdv_create_delivery_table ( $id ){ global $wpdb; $...
user avatar
0 votes
0 answers
23 views

Add posts to custom table in database instead of wp_posts

What the code does: Imports posts, loops through all the posts and calls wp_insert_post to create a new posts for each import. Objective: Import posts and store them in another table, not under the ...
user avatar
  • 177
0 votes
1 answer
29 views

how to get serialized post meta

I have posts that have video content and these videos stored in database like this (serialized) a:1:{s:9:"video_url";s:0:"";} This serialized data have post video urls but can ...
user avatar
1 vote
2 answers
40 views

wpdb get_results() and prepare when to use prepare?

so if i have a function that gets terms from the database ( not the user ) do I need to use prepare first ( before get_results() ), or some sort of data sanitizing?
user avatar
0 votes
1 answer
18 views

How to left join meta in queries [closed]

I have a query something like //read products with thumbnail and sku SELECT products.*, pm1.meta_value as sku, p1.guid as thumbnail FROM `wp_posts` as products LEFT JOIN `wp_postmeta` as pm1 ...
user avatar
  • 111
0 votes
1 answer
33 views

Isn't Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}

I'm trying to count rows in sql table using SELECT COUNT(*) for the post view count. My code is function get_post_views($offset=0, $post_id){ global $wpdb; $get_visit_query = "...
user avatar
0 votes
0 answers
31 views

wordpress mysql / wpdb ajax load more for terms help

Can anyone pls assist me with my problem. I am not asking for code, but even some guidance as to how to get this working. I have tried many ways, to get my terms to load using ajax load more and seem ...
user avatar
  • 16
0 votes
0 answers
26 views

Get child product categories from parent product category for CUSTOM TAXONOMY archive page

Can anyone help me as LoizTheAztec pretty much covered most of what I need here with his very helpful answers. What I need to do is alter this further, and rather than it being on a Product Cat ...
user avatar
  • 16
0 votes
0 answers
24 views

Improve performance of slow query

I'm using this query to obtain all the products buyed by user and works but query monitor says me that is a slow query (above 0,5 sec), there is a way to fix this? Here the db query: $result = $wpdb-&...
user avatar
0 votes
1 answer
31 views

Sql formatting for post data within function

I'm calling a function through ajax in my site, and within the function that's being hit I'm taking the data (in this case it's a search term) and putting it into LIKE statements in sql: $...
user avatar
  • 113
1 vote
0 answers
19 views

Creating mySQL procedure with $wpdb

I'm trying to create a calendar table upon activation of a custom plugin I am building. The procedure script I am using can be seen here: https://gist.github.com/bryhal/4129042 The first part that ...
user avatar
1 vote
1 answer
30 views

WPDB If primary key already exists, add +1 to integer field

I need a system in which I check if a Primary key (string) already exists. If it does, then I want to add 1 to a second column. If it doesn't, then I want to add a new row and set the second column ...
user avatar
0 votes
1 answer
29 views

get specific values from WordPress meta_value

a:1:{s:9:"toys_item";a:1:{s:16:"toys_item_ranger";a:11:{i:0;s:31:"2 x Seadoo GTX 230/IBR Jet-Skis";i:1;s:30:"1 x HD Runway 10′ Waterslide";i:2;s:14:"4 x F5 ...
user avatar
0 votes
1 answer
45 views

$wpdb->update() always need a second try

I will use a custom endpoint of REST APi to update fields in a custom table. Therefore, i wrote this function: function update_table_single($request = null){ global $wpdb; $data_key = trim($...
user avatar
  • 103
0 votes
1 answer
56 views

How to run wp_insert_post() & wpdb on the background?

I have created a code to fetch data from a remote API then echo certain data & import those data into a post type using wp_insert_post() & post meta** using wpdb->query(). Here's my current ...
user avatar
0 votes
0 answers
41 views

Counting Posts with multiple taxonomy and terms condition using MySQL and AJAX

I am trying to get the number of posts with multiple taxonomy and terms using MySQL and AJAX. but I cannot get the desired result with my query. I derived my query in this wpdb query and sadly it ...
user avatar
1 vote
1 answer
120 views

Why is variable not working on custom sql query using wpdb?

Im trying to insert bulk post meta in a single query, however when it wont insert data if i use variable on it. Here are my codes so far. I added comments on which codes work & which does not. (...
user avatar
0 votes
1 answer
52 views

Passing in MySQL prepare statement parameter separately throwing error

I have some code that looks like this: $stmt = $wpdb->prepare("SELECT * FROM <tablename> WHERE user_id = %d", $user_ID); It seems to work fine... However, I have some queries that ...
user avatar
0 votes
0 answers
16 views

Convert a column of a table containing an Array as response in HTML

I am trying to iterate through the following information saved in a table in my database: wp_table_log id_envio int(11) order_id int(11) fecha datetime request text response text ...
user avatar
0 votes
1 answer
80 views

Which is faster wpdb & get_row or wp_query & ge_post_meta?

Im planning to make a private api and i was wondering which one should i use go for my datas. wpdb & get_row with my custom sql regular wp_query & get_post_meta() Im wondering if it's better ...
user avatar
0 votes
0 answers
93 views

Database SQL query error

I am trying to run below query. $sql = "SELECT * FROM `wp_podsrel` WHERE `related_item_id` =$user->ID AND `pod_id` = 17 AND `field_id` = 25"; $results = $wpdb->get_results($sql) or ...
user avatar
  • 377
0 votes
0 answers
40 views

$wpdb->insert not inserting all rows

I'm doing a for loop and in the loop inserting items into a custom table in WP. I did some logging and it is inserting each post(returning a 1 for true) and even returning an ID, but when I look in ...
user avatar
0 votes
0 answers
47 views

How to pass an input value into wpdb->Prepare

If I hardcode $find = 'Feb', I get results, if $find = $searchq, I get Feb searchq Birthdays in Feb Feb find global $wpdb; $path = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/twentytwelve-...
user avatar
0 votes
1 answer
334 views

ob_end_flush(): failed to send buffer of zlib output compression (0) in external php

I have this little php I call from jqGrid url that I use inside an admin's wordpress plugin: require_once($_SERVER['DOCUMENT_ROOT'] . '/xDl21my20/wp-load.php'); remove_action( 'shutdown', '...
user avatar
  • 103
0 votes
0 answers
36 views

wpdb insert formatting for date in array

I have this simple wpdb->insert: $this->wpdb->insert( 'tablename', array(...., field1 => 'blahblah' , 'birthdate' => (isset($this->dob)) ? esc_attr(date('Y-m-d', strtotime($this-&...
user avatar
  • 103
0 votes
1 answer
62 views

See if the email exists or not

I have extracted a bunch of emails using PHP IMAP connection and assigned those emails to a custom post type. Here's the code how I assigned those emails to custom post type. $total = $emails->...
user avatar
0 votes
0 answers
22 views

How capturate wpdb exceptions?

If, in a function. I execute this (wrong) query: try { return $this->wpdb->get_var('SELECT * from mickeymouse'); //intentional bug: there isn't a table "mickeymouse" } catch(\...
user avatar
0 votes
1 answer
63 views

Search and replace not working for ACF Images

I just connected a staging site from IP Address to a live domain. I ran search and replace on everything from IP Address to domain name. I mistakenly added the trailing slash after the domain name ...
user avatar
  • 152
0 votes
0 answers
21 views

How do I get specific readable results from this query and array results

I am running the following query to get an array of WP revisions which includes custom fields revisions. This seems to be working and spitting out the array and there are two values I am wanting to ...
user avatar
  • 1
0 votes
2 answers
128 views

Conditional sorting with variable using WP Query

I have for example this posts in WP: Name ID Date JohnA 6 01012020 JohnB 7 01022020 KateA 7 05052021 SamA 4 01022020 SamA 4 01022022 How I can order by date, but also put posts with ...
user avatar
1 vote
1 answer
131 views

Wp_query function to search from product_title 'OR' product tags name

I am looking to show search results from either product title or product tag name. I have implemented the product title search through the below code, but I need to add tag name search also into it. ...
user avatar
0 votes
0 answers
32 views

Wordpress SQL Prepare

i'm trying to adapt this code to also take in to account specific categories and custom post types, so it would give me select dropdown populated with unique custom field values, from a specific post ...
user avatar
0 votes
0 answers
31 views

How to display specific data from a custom table to logged in users with a custom role

I have constructed a custom plugin that stores data in a custom DB table relating to the viewing activity of users who have a custom role of 'attendee' on a custom post type page called 'presentations'...
user avatar
0 votes
0 answers
94 views

duplicated posts when using pagination

This is my query, it's so simple (the query is generated by canvas plugin) ps: the $page has correct value $query_var = [ 'post_type' => 'new', 'orderby' => 'date', 'order' => 'DESC', 'paged' ...
user avatar
0 votes
0 answers
25 views

Passing a SQL query to the WP Query

I'm writing this filter and would like your help in fixing the following this code. I will like to take the SQL statement below and place it in a WP query. select post_id from wp_postmeta WHERE ...
user avatar
1 vote
0 answers
12 views

$wpdb->update with multiple parameters gives error

can this be simplified into one query using $wpdb->update? global $wpdb; $wpdb->update( $wpdb->users, array('user_login' => $new_user_email), array('ID' => $user_id) ); //...
user avatar
  • 11
0 votes
1 answer
33 views

Register custom table for WP to use in a plugin

I am trying to add a custom table handler to $wpdb. public function __construct() { $this->register_tables(); } public function register_tables() { global $wpdb; $wpdb->tables[] = '...
user avatar
0 votes
1 answer
55 views

$wpbd->insert_id is returning null

I have been trying to return a $wpdb->insert_id; but unfortunately, it is returning null; What am I doing wrong here? I am sending data using ajax from the front end. Here is the backend code. ...
user avatar
0 votes
2 answers
52 views

wpdb->insert with special chars failing with collation utf8mb4_unicode_520_ci

I'm performing an wpdb->insert(). When a field includes a special the insert is not done and I get the error Error in Wordpress database for query SHOW FULL COLUMNS FROM wp_my_table If I don't ...
user avatar
0 votes
2 answers
67 views

Is it safe to access the underlying mysqli object from \wpdb for custom queries?

I am writing a custom (bespoke, for 1 client, not for publication and general consumption) plugin, that needs to access custom tables. In general with WP,I follow the principle of using the higher ...
user avatar
  • 201
2 votes
2 answers
143 views

Get updated post meta on save_post action?

I'm trying to save post id and keyword (Yoast Focus Keyword) to a custom table when post is created or updated via save_post action. It saves everything fine on Publish (first-time) but when I update ...
user avatar
  • 125
0 votes
1 answer
106 views

How to escape percentage sign(%) in sql query with $wpdb->prepare?

I have the following code $v12 = 'Milk(45% Fett i.Tr.)'; $sql = "INSERT INTO diary_data SET v2= '0.2', v12=$v12"; $final_sql = $wpdb->prepare( $sql, ''); but after echoing $final_sql ...
user avatar
0 votes
0 answers
113 views

MySQL 'duplicate entry' error

Why am I getting an error, and how do I fix it? This is how I create a table: $wpdb->query(" CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}tdlrm_references`( ID bigint(20) unsigned ...
user avatar
  • 63
0 votes
1 answer
40 views

How to get last user with wpdb?

can someone please help me with this query: $ultimousuario = $wpdb->get_var( "SELECT TOP 1 FROM $wpdb->users WHERE user_login = 'usuario*' ORDER BY user_login DESC"); echo "<p&...
user avatar
0 votes
0 answers
57 views

wpdb Insert unknown post data dynamic foreach loop

I have this old function which I would like to use in WordPress. public function database_insert( $table, $data ) { $data_key = "("; $data_value = "("; $i = ...
user avatar
0 votes
0 answers
52 views

wpdb update at most one row

I would like to update a row using wpdb update, but always one row precisely. My concern is that in this case targeting the row will require multiple parameters in the where clause, and should the ...
user avatar
0 votes
1 answer
79 views

Why does wpdb->update delete other meta?

My existing data in the termmeta table: meta_id term_id meta_key meta_value 27374 546 1C_id 9095b4cf-969d-11e9-a601-5cf3706390c8 27918 546 tax_position 1 My code: $found_id = $wpdb->get_var(&...
user avatar
  • 63
0 votes
1 answer
123 views

Get data from custom table and update relative post_meta based on meta_key

long story short: I need to retrieve data from WordPress-created custom Table and add/update metadata to the related post (post_type=product). I can match the posts (products) and custom table fields ...
user avatar

1
2 3 4 5
24