I see the error described above. I am assuming I need to do a foreach or while command – but all attempts to use these have not yielded results.
Yes, shortcodes return strings, not arrays/lists. You need to convert your array/list into a single string. However, get_var
assumes a single result. If you want multiple results you will need to use a different method of WPDB that allows multiple results, such as get_results
:
<?php
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT );
The code also needs to use $wpdb->prepare
to insert variables into the SQL statement. E.g.:
$wpdb->query(
$wpdb->prepare(
"
INSERT INTO $wpdb->postmeta
( post_id, meta_key, meta_value )
VALUES ( %d, %s, %s )
",
10,
$metakey,
$metavalue
)
);
Notice that the $metakey
and $metavalue
variables are not inside the string as that would be insecure. Instead they have placeholders %s
that get replaced with sanitised versions that prevent them from breaking the query or inserting malicious values