SQL syntax error. However, it works normally at phpmyadmin

) bracket missing in last condition. (i.e AND (b.meta_key = ‘usage_count’ AND b.meta_value=”0″”); ) $rowcount = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->postmeta AS a, $wpdb->postmeta AS b WHERE a.post_id = b.post_id AND (a.meta_key = ‘customer_email’ AND a.meta_value LIKE ‘%[email protected]%’) AND (b.meta_key = ‘usage_count’ AND b.meta_value=”0″)”); echo $rowcount;

Get posts from category with custom query

It looks like this is the new part of this query causing the problem, is that right? (SELECT term_taxonomy_id FROM wp_BOMEGAterm_relationships WHERE object_id = p.ID) AS categories If so the error message is clear: a sub-select in this query should only return one row. If it returns more than one row it doesn’t know how … Read more

Run Username SQL Query from WordPress Child Theme Functions File

There are many ways you can achieve this… In your instance, as you pointed out $wpdb, here is the possible ways you can achieve this: 1. Use $wpdb::query You can run this query directly using $wpdb’s query method as: add_action(“init”, “username_query”) function username_query(){ global $wpdb; $prefix = $wpdb->prefix; $query = “UPDATE `{$prefix}wp_users` SET `user_login`= ‘new-admin-name-here’ … Read more

How to list all categories and their IDs using SQL query?

I finally got the solution. Below SQL Query gets the list of all categories with it’s ID from WordPress table – SELECT wp_term_taxonomy.term_id, wp_terms.name FROM wp_term_relationships LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) LEFT JOIN wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_taxonomy_id) WHERE wp_term_taxonomy.taxonomy = ‘category’ GROUP BY wp_term_taxonomy.term_id order by wp_terms.name Hope this could help others.