Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want display authors'posts with the code below but authors' name aren't showing. What is wrong?

<? // query test
$qry = $wpdb->get_results( $wpdb->prepare("
SELECT *
FROM $wpdb->posts
WHERE post_type ='post'
AND post_status='publish'
order by 'ID' DESC
LIMIT 5
"));

echo "<table width=100% cellspacing=1 cellpadding=5 bgColor='#cccccc'>";

foreach ( $qry AS $k => $v ){

$qry_name = $wpdb->get_results( $wpdb->prepare("
SELECT user_nicename AS USER_NAME
FROM 'wp-users' AS a
LEFT JOIN 'wp-posts' AS b
ON a.ID = b.post_author
WHERE b.post_author = a.ID
"));

$ID = $v->ID;
$title = stripslashes(mb_substr($v->post_title, 0,20,"utf-8"));
$ddate = $v->post_modified;
$name = $v->user_nicename;
$link = '<a href=/archives/'.$ID.'>';
$list = "<tr><td bgColor='#ffffff'>".$ID."</td><td bgColor='#ffffff'>".$link.$target.$title."</a></td><td bgColor='#ffffff'> on ".$ddate."</td><td bgColor='#ffffff'>".$qry_name[0]->USER_NAME."</td></tr>";
echo $list;
}

echo "</table>";
?>
share|improve this question

1 Answer 1

Your table names are wrong. note in your first query the use of $wpdb->posts to reference the posts table.

however- this is a very simple query that can be done via the API rather than writing SQL queries from scratch. Use a new WP_Query instance instead.

share|improve this answer
    
Thanks a lot. resolved. –  JayteeHong Aug 16 '12 at 2:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.