I am having issues returning a valid response from the below code. The intention is to return a list of cities based upon post_title().
The print_r response indicates a response is being processed (see below) however when an echo on $city is undertaken it sends an error.
add_shortcode('citylist',function(){
global $wpdb;
$country = $wpdb->prefix . 'worldcities';
$title = wp_title("",false,'right');
$args = $wpdb->get_results( "SELECT city_ascii FROM $country WHERE ranking < 2 AND country = '$title'");
foreach ($args as $city_ascii=>$city) {
print_r ($city);;
};
} );
The print _r response is as follows (note Page_Title is "Nigeria".
StdClass Object ([city_ascii] => Ibadan) StdClass Object ([city_ascii] => Ogbomoso ...
All of the responses are expected and correct (ie. Ibadan, Ogbomoso etc).
However when this is replaced with echo, it throws an error. (see revised code below).
add_shortcode('citylist',function(){
global $wpdb;
$country = $wpdb->prefix . 'worldcities';
$title = wp_title("",false,'right');
$args = $wpdb->get_results( "SELECT city_ascii FROM $country WHERE ranking < 2 AND country = '$title'");
foreach ($args as $city_ascii=>$city) {
echo $city;;
};
} );
Any help would be appreciated.
(Note I have read the PHP manuals, reviewed previous posts here and on other boards already before anyone suggests this avenue).
I am having issues returning a valid response from the below code. The intention is to return a list of cities based upon post_title().
The print_r response indicates a response is being processed (see below) however when an echo on $city is undertaken it sends an error.
add_shortcode('citylist',function(){
global $wpdb;
$country = $wpdb->prefix . 'worldcities';
$title = wp_title("",false,'right');
$args = $wpdb->get_results( "SELECT city_ascii FROM $country WHERE ranking < 2 AND country = '$title'");
foreach ($args as $city_ascii=>$city) {
print_r ($city);;
};
} );
The print _r response is as follows (note Page_Title is "Nigeria".
StdClass Object ([city_ascii] => Ibadan) StdClass Object ([city_ascii] => Ogbomoso ...
All of the responses are expected and correct (ie. Ibadan, Ogbomoso etc).
However when this is replaced with echo, it throws an error. (see revised code below).
add_shortcode('citylist',function(){
global $wpdb;
$country = $wpdb->prefix . 'worldcities';
$title = wp_title("",false,'right');
$args = $wpdb->get_results( "SELECT city_ascii FROM $country WHERE ranking < 2 AND country = '$title'");
foreach ($args as $city_ascii=>$city) {
echo $city;;
};
} );
Any help would be appreciated.
(Note I have read the PHP manuals, reviewed previous posts here and on other boards already before anyone suggests this avenue).
Share Improve this question asked Jan 9, 2021 at 22:11 BretMaddenBretMadden 31 bronze badge 3 |1 Answer
Reset to default 0Your print_r()
statement shows that your $wpdb
call is returning an array of objects, and you can't echo
an object. (Unless the object has a __toString()
method, but stdClass
doesn't.)
You need to get the property you seek from the object, like this: $city->city_ascii
.
Also, you shouldn't echo
the content generated by a shortcode. You should instead return
the string.
Code edited to use $wpdb->prepare()
to protect against SQL injection (per @TomJNowell's comment).
add_shortcode('citylist',function(){
global $wpdb;
$content = '';
$country = $wpdb->prefix . 'worldcities';
$title = wp_title("",false,'right');
$args = $wpdb->get_results(
$wpdb->prepare(
"SELECT city_ascii FROM %s WHERE ranking < 2 AND country = %s",
$country,
$title
)
);
foreach ($args as $city_ascii=>$city) {
$content .= esc_html( $city->city_ascii ) . '<br />';
};
return $content;
} );
echo $city[0];
orecho $city_ascii[0];
? Also, what do you see when you change the foreach toforeach ($args as $city_ascii) {
? – Celso Bessa Commented Jan 9, 2021 at 22:20$wpdb->prepare
to sanitise and validate your variables instead of just including them directly – Tom J Nowell ♦ Commented Jan 9, 2021 at 23:27