Wordpress SQL Select Multiple Meta Values / Meta Keys / Custom Fields
Posted
by Wes
on Stack Overflow
See other posts from Stack Overflow
or by Wes
Published on 2010-03-28T03:35:38Z
Indexed on
2010/03/28
3:43 UTC
Read the original article
Hit count: 755
I am trying to modify a wordpress / MySQL function to display a little more information. I'm currently running the following query that selects the post, joins the 'postmeta' and gets the info where the meta_key = _liked
function most_liked_posts($numberOf, $before, $after, $show_count) {
global $wpdb;
$request = "SELECT ID, post_title, meta_value FROM $wpdb->posts, $wpdb->postmeta";
$request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
$request .= " AND post_status='publish' AND post_type='post' AND meta_key='_liked' ";
$request .= " ORDER BY $wpdb->postmeta.meta_value+0 DESC LIMIT $numberOf";
$posts = $wpdb->get_results($request);
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$permalink = get_permalink($post->ID);
$post_count = $post->meta_value;
echo $before.'<a href="' . $permalink . '" title="' . $post_title.'" rel="nofollow">' . $post_title . '</a>';
echo $show_count == '1' ? ' ('.$post_count.')' : '';
echo $after;
}
}
The important part being: $post_count = $post->meta_value;
But now I want to also grab a value that is attached to each post called wbphoto
How do I specify that $post_count = _liked and $photo = wbphoto
?
Here is a screen cap of my Phpmyadmin
© Stack Overflow or respective owner