Title: WPDB query using an array
Last modified: August 21, 2016

---

# WPDB query using an array

 *  [joshdd](https://wordpress.org/support/users/joshdd/)
 * (@joshdd)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/wpdb-query-using-an-array/)
 * Okay, so I use a wpdb->get_results that returns an array ($termtax) from one 
   table.
    I then want to use this array in a WHERE clause in a wpdb->get_var
 *     ```
       WHERE term_taxonomy_id = '".$termtax array."'
       AND taxonomy = 'wp_prc'"
       ```
   
 * how would I do this?
 * Thanks, Josh.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/wpdb-query-using-an-array/#post-3705716)
 * I only know enough about this to be dangerous, so take with a grain of salt. 
   The basic form is `...WHERE term_taxonomy_id IN ('$termtax2') AND...`, but the
   real issue is $termtax2 cannot be a PHP array. The $termtax array needs to be
   imploded into a comma delimited string.
    `$termtax2 = implode(', ', $termtax);`
   I hope I got that right, something along those lines at least, good luck.
 *  Thread Starter [joshdd](https://wordpress.org/support/users/joshdd/)
 * (@joshdd)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/wpdb-query-using-an-array/#post-3705750)
 * Hey bcworkz, thanks for the reply, I did actually work this out already, and 
   you’re pretty much right, I actually had to flatten the array first, then implode
   it, but yeah, other than that, you’re pretty much right, I’ll come back and post
   the code in a minute.
 * Thanks, Josh.
 *  Thread Starter [joshdd](https://wordpress.org/support/users/joshdd/)
 * (@joshdd)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/wpdb-query-using-an-array/#post-3705751)
 *     ```
       $termtax = $wpdb->get_results(
       		"SELECT term_taxonomy_id
       		FROM wp_term_relationships
       		WHERE object_id = '".$postid."'",
       		ARRAY_N
       );
   
       function array_values_recursive($thearray)
       {
           $newtermtax = array();
   
           foreach ($thearray as $value)
           {
               if (is_scalar($value) OR is_resource($value))
               {
                    $newtermtax[] = $value;
               }
               elseif (is_array($value))
               {
                    $newtermtax = array_merge($newtermtax, array_values_recursive($value));
               }
           }
   
           return $newtermtax;
       }
   
       $newnewtermtax = implode(", ", array_values_recursive($termtax));
   
       $termid = $wpdb->get_var(
       		"SELECT term_id
       		FROM wp_term_taxonomy
       		WHERE term_taxonomy_id
       		IN (".$newnewtermtax.")
       		AND  taxonomy =  'wpfc_prc"
       );
       ```
   
 * Obviously you have to use backticks around all the tables column names.
 * Also, anyone reading this to help them, remember, when inserting a string into
   a table do not forget to use addslashes() to ensure all characters that need 
   to be ‘escaped’, are escaped.
    This is a noob error, and one that took me 20mins
   to realise I had made.
 * Thanks, Josh.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘WPDB query using an array’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [database](https://wordpress.org/support/topic-tag/database/)
 * [get_results](https://wordpress.org/support/topic-tag/get_results/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [where](https://wordpress.org/support/topic-tag/where/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 2 participants
 * Last reply from: [joshdd](https://wordpress.org/support/users/joshdd/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/wpdb-query-using-an-array/#post-3705751)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
