• 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

    (@bcworkz)

    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

    (@joshdd)

    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

    (@joshdd)

    $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.