WPDB SQL Ignore `post_status` Parameter

Well, first of all, you use where post_status=”publish”. So you wouldnt need to explicitly tell the drafts not to show up by using wp_posts.post_status != ‘draft’ I think, your SQL says somthing like: Grab all posts which are published and not drafted and attached to term_id 17 OR grab all posts which are attached to … Read more

wpdb and acf via wp rest api

I ended up using $wpdb for my custom tables and went with wp_query for my custom posts with ACF fields $q = new WP_Query(array( ‘post_type’ => ‘coupon’, ‘post__in’ => $cids )); $coupons = array(); while($q->have_posts()) : $q->the_post(); $p = get_post(); $f = get_fields(); $m = array_merge((array)$p, (array)$f); array_push($coupons, $m); endwhile; And that’s how the cookie … Read more

wpdb query not returning results containing single quotes

Here is an example of performing a LIKE based query while escpaing the value parameter passed to the query: $results = $wpdb->get_results( $wpdb->prepare( “SELECT post_title, ID from $wpdb->posts WHERE post_title LIKE ‘%%%s%%'”, $wpdb->esc_like(“today’s weather is nice”) ) ); Update: Based on your edit, your query is incorrectly formed hence the error, it should be: global … Read more

Why is this $wpdb query looping 5 times?

The most likely cause is that the hook is probably running multiple times (e.g. once per meta field). You may need to qualify/filter your function like so… add_action( ‘added_post_meta’, function ( $mid, $object_id, $meta_key, $_meta_value) { global $wpdb; if ( $meta_key != ‘data19’ ) { return; } $table_name = $wpdb->prefix . ‘postmeta’; $results = $wpdb->get_var( … Read more

Using “->” in a page to exceute $wpdb query gives error

Code is Code and content is content and they should not be mixed together. the editor is not a code editor, the content can and usually will be transformed in a way you don’t expect which might result in your code failing with no obvious reason. Just don’t do it. Write your own shortcode that … Read more

WordPress wpdb->insert returns int(0) => doesn’t insert anything, no errors!

Your format is wrong. On your code is should be $insertion = $wpdb->insert($wpdb->prefix . ‘table-name-of-plugin’, array( ‘column-name’ => $stringValueForC1, ‘second-column-name’ => $stringValueForC2 ), array(‘%s’, ‘%s’) ); Notice the change from array(‘%s, %s’) to array(‘%s’, ‘%s’) Also, since you are setting the format for both of the value as string. I recommend that you just use … Read more

Can’t call WPDB inside RSS template

After discussing we’ve discovered the problem isn’t directly with calling $wpdb->get_row but a mismatch between the data it returns and the processing in the foreach loop. For a single row of data, replacing… foreach($beforeAfter as $imageSet) { ?> <item> <title><?php echo get_the_title($imageSet->ID); ?></title> </item> <?php } … with … <item> <title><?php echo get_the_title($imageSet->ID); ?></title> </item> … Read more

Getting values from wpdb

You are not too far away from your goal. The get_calendar_ids must return only $calendars. In your function $wpdb->get_results( $sql ) will return an object with the datas your asked. function get_calendar_ids() { global $wpdb; $user_id = get_current_user_id(); $sql = $wpdb->prepare( ” Select wp_calendars.user_id, wp_calendars.name, wp_calendars.id From wp_calendars Where wp_calendars.user_id = ‘$user_id’ “); $calendars = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)