thanks for looking at my issue. So I am trying to return each term of the five that my query is selecting. Here is my function:
function VD_top_terms(){
global $wpdb;
$VDtermsSQL = "SELECT DISTINCT AN_terms.name from AN_posts INNER JOIN AN_term_relationships on AN_term_relationships.object_id = AN_posts.ID INNER JOIN AN_term_taxonomy on AN_term_taxonomy.term_taxonomy_id=AN_term_relationships.term_taxonomy_id INNER JOIN AN_terms on AN_term_taxonomy.term_id=AN_terms.term_id where AN_posts.post_date_gmt >= NOW() - INTERVAL 1 DAY and AN_term_taxonomy.taxonomy ='post_tag' group by AN_terms.name order by count DESC limit 5";
$VDtResults = $wpdb->get_results($VDtermsSQL, ARRAY_A);
foreach( $VDtResults as $VDsinTerm) {
echo $VDsinTerm;
}
}
This is the response:
ArrayArrayArrayArrayArray
Not sure where I am going wrong here. If i remove the foreach and echo statement and substitute it for a printr statement I get the following which is why I switched over to the foreach loop and added ARRAY_A.
Array ( [0] => stdClass Object ( [name] => Cat ) [1] => stdClass Object ( [name] => Door ) [2] => stdClass Object ( [name] => School ) [3] => stdClass Object ( [name] => falls ) [4] => stdClass Object ( [name] => Court ) )
I am trying to get the results Cat, Door, School, Falls, Court to display on the page. Any help would be appreciated. Thanks
My Answer:
The issue is that $VDsinTerm is an array, not a string. You need to access the 'name' key of each array to get the term value. Here's the updated code:
php
function VD_top_terms(){
global $wpdb;
$VDtermsSQL = "SELECT DISTINCT AN_terms.name from AN_posts INNER JOIN AN_term_relationships on AN_term_relationships.object_id = AN_posts.ID INNER JOIN AN_term_taxonomy on AN_term_taxonomy.term_taxonomy_id=AN_term_relationships.term_taxonomy_id INNER JOIN AN_terms on AN_term_taxonomy.term_id=AN_terms.term_id where AN_posts.post_date_gmt >= NOW() - INTERVAL 1 DAY and AN_term_taxonomy.taxonomy ='post_tag' group by AN_terms.name order by count DESC limit 5";
$VDtResults = $wpdb->get_results($VDtermsSQL, ARRAY_A);
foreach( $VDtResults as $VDsinTerm) {
echo $VDsinTerm['name'] . "
";
}
}
This should now display the terms Cat, Door, School, Falls, Court on the page.
Rate this post
5 of 5 based on 1759 votesComments