0

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>";
?>

1 Answer 1

3

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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