get the number of queries made on a page(inside my plugin)

$wpdb counts the queries ran and saves the value in the $num_queries class variable. $num_queries The number of queries that have been executed. https://codex.wordpress.org/Class_Reference/wpdb All you need to do is retrieve it: echo $wpdb->num_queries; You may also be interested in the $queries class variable. $queries You may save all of the queries run on the … Read more

How to prevent $wpdb->prepare stripping a leading zero in variable value?

Try this: $order_id = 012345; $sql = $wpdb->prepare(“SELECT payStatus FROM test_table WHERE ref = %s”,$order_id);//<– this doesn’t work var_dump($sql); echo ‘<br/>’; $sql = $wpdb->prepare(“SELECT payStatus FROM test_table WHERE ref = %d”,$order_id);//<– this doesn’t work var_dump($sql); echo ‘<br/>’; $sql = $wpdb->prepare(“SELECT payStatus FROM test_table WHERE ref = %f”,$order_id);//<– this doesn’t work var_dump($sql); echo ‘<br/>’; $sql = … Read more

Need help writing a $wpdb query

Is this what you’re trying to do? // WP_Query arguments $args = array ( ‘post_type’ => array( ‘project’ ), ‘meta_query’ => array( array( ‘key’ => ‘meta_key’, ‘value’ => ‘%$query%’, ), ), ); // The Query $query = new WP_Query( $args );

Why doesn’t my insert query work?

You have the following for the table name: $wpdb->prefix . “wp_list_press” Check your actual prefix and table name. In a WP installation using “wp_” as the prefix, the above would result in “wp_wp_list_press”. Is that the table name in the db or is it just “wp_list_press”? If the table in the db is “wp_list_press” then … Read more

Replicating the WP_Query ‘s’ param with $wpdb

It seems that you in your INNER JOIN, you are joining on wp_term_relationships.object_id == wp_posts.post_title which is erroneous, since object_id is the post-id in this case-scenario. I want all IDs (post-ids) SELECT ID FROM $wpdb->posts Who have a record in the wp_term_relationships table // Now we are comparing the correct columns INNER JOIN $wpdb->term_relationships ON … Read more

How to Modify this $wpdb query to accept an array of post statuses

Use the IN operator for arrays. When using $wpdb->prepare() you don’t have to use quotes around the string placeholder %s. The string placeholder will be quoted automatically. That’s also the reason why you have to take precautions when using $wpdb->prepare() with the IN operator if the array values are strings. This first example doesn’t use … Read more

How to fetch an array in $wpdb?

You have a problem with your query which is you can’t use GROUP BY and ORDER BY before WHERE replace FROM tablename GROUP BY submit_time ORDER BY submit_time DESC with FROM tablename This is your query $results = $wpdb->get_results( ‘SELECT DATE_FORMAT(FROM_UNIXTIME(submit_time), “%b %e, %Y %l:%i %p”) AS Submitted, MAX(IF(field_name=”Name”, field_value, NULL )) AS “Name”, MAX(IF(field_name=”Email”, … Read more

Create an array with a string key from wpdb->get_results

Found the solution: $currentProducts = $wpdb->get_results(“SELECT feedid, id, size, price FROM products WHERE shopid = $shopid”, OBJECT_K); The OBJECT_K parameter makes an associative array with feedid as key: https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results