How to use MySQL 'IN' Operator in prepare statement in WordPress - Long colorful lines of code on a computer screen
How to use MySQL 'IN' Operator in prepare statement in WordPress - Photo by Markus Spiske on Unsplash

How to use MySQL ‘IN’ Operator in prepare statement in WordPress

This is how to use MySQL IN Operator in prepare statement in WordPress. Let me straight away give you the code.

How to use MySQL IN Operator in prepare statement in WordPress
How to use MySQL ‘IN’ Operator in prepare statement in WordPress – Photo by Kevin Ku on Unsplash
global $wpdb;

$agenda_id_arr = $wpdb->get_results( $wpdb->prepare( 'SELECT agenda_id 
                 FROM ' . $agenda_bookmark_tbl . ' WHERE user_id = 
                 %d', $user_id ), "ARRAY_A" );

foreach( $agenda_id_arr as $agenda_id ){
    $agenda_ids[] = $agenda_id['agenda_id'];
}

$in = implode(',', array_fill(0, count($agenda_ids), '%d'));

$agenda_list_arr = $wpdb->get_results( $wpdb->prepare( ' SELECT * FROM 
                   ' . $agenda_tbl . ' WHERE id in ('. $in .') ', 
                   $agenda_ids), "ARRAY_A" );

Listed a few important links to refer.

Was that a useful Article? Let me know your comments & feedback. Do I need any edits in the code?

Other Popular Reads

About the author

Rohan Kamble

A Web Developer by profession, who loves the web to the core with over 9 Years of Experience working on a wide range of domains like websites on e-commerce, Travel, Alumni, matrimonial, company's professional sites, Finance, company internal communications and many more. Also WordPress Plugins & Chrome extensions.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.