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

Huge thanks to @kero !! I was finally able to create a solution by making the sql statement a variable first (as kero suggested) then using that variable on wpdb->query(); Here’s my updated and fully working codes now: $imdbid = $_GET[‘id’]; $string = file_get_contents(“http://www.omdbapi.com/?i=”.$imdbid.”&apikey=9a187152″); $json = json_decode($string, true); if( $json[‘Response’] === “True” ){ $dataID = … Read more

MySql database help for a plugin

Hi @ntechi: I think this is what you are looking for: $sql =<<< SQL SELECT id FROM myrp_ratings AS rat, INNER JOIN wp_comments AS com ON com.comment_ID = rat.comment_id WHERE 1=1 AND com.comment_author_email=”%s” AND rat.value != ‘0’ AND rat.value != ‘1’ LIMIT 1 SQL; $sql = $wpdb->prepare($sql,’[email protected]’); $id = $wpdb->get_var($sql); if (is_null($id)) { $wpdb->query(“INSERT INTO … Read more

Tags to Post-ID mysql query. Tag Search

These are normal IN mechanics – it matches anything in set, not all of set combined. Type of match you want is called tag_slug__and in WP query arguments. You can see code that generates SQL for it in source of WP_Query->&get_posts() method. Resulting SQL is like this: SELECT p.ID FROM wp_posts p INNER JOIN wp_term_relationships … Read more

CRUD operations using WordPress database API

Querying in WordPress is done by using the the wpdb variable. Be sure to always prepare your sql so WP can make it database safe. Example: global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( “SELECT * FROM `{$wpdb->prefix}TABLE_NAME` WHERE `var` = %d”, 1) ); Then you can loop through the data and output it as for example … Read more

Distinction on meta value on pre_get_posts

DISCLAIMER : Not a WordPress Expert, Just an old MySQL DBA Here is your original generated SQL SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type=”post” AND (wp_posts.post_status=”publish” OR wp_posts.post_status=”private”) AND ( ( wp_postmeta.meta_key = ‘_position’ AND CAST(wp_postmeta.meta_value AS CHAR) > ‘0’ ) ) GROUP BY wp_posts.ID, wp_postmeta.meta_value … Read more

$wpdb: Counting posts corresponding to 3 terms in 3 different taxonomies

Do a tax-query and then count the result. No need for a custom query with dozens of JOINS. $posts = new WP_Query( array( ‘post_type’ => ‘MYPOSTTYPE’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘TAX_1’, ‘field’ => ‘slug’, ‘terms’ => array( ‘TERM_TAX_1’ ), ‘operator’ => ‘IN’ ), array( // etc. ) ), ‘post_status’ => ‘publish’ ); prinft( … Read more

For best DB performance should I serialize all theme options together or by type?

All options with autoload = yes (default) are fetched very early in one query. So the number of options does affect performance only marginally. Split options if you don’t need everything on every page load. With … add_option( ‘option_name’, ‘option_value’, ”, ‘no’ ); … you can set options that aren’t loaded before you actually call … Read more