我正在尝试修改wordpress / MySQL功能以显示更多信息。我目前正在运行以下查询选择帖子,加入'postmeta'并获取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;
}
}
重要的部分是:$post_count = $post->meta_value;
但是现在我还要抓取附加到每个帖子wbphoto
如何指定 $ post_count = _liked和 $ photo = wbphoto
这是我的Phpmyadmin的屏幕截图 alt text http://i44.tinypic.com/ioisuv.png
答案 0 :(得分:2)
SQL看起来很丑陋。
// Your Meta key names
$metas = array(
'_liked', '_another1'
);
foreach ($metas as $i=>$meta_key) {
$meta_fields[] = 'm' . $i . '.meta_value as ' . $meta_key;
$meta_joins[] = ' left join ' . $wpdb->postmeta . ' as m' . $i . ' on m' . $i . '.post_id=' . $wpdb->posts . '.ID and m' . $i . '.meta_key="' . $meta_key . '"';
}
$request = "SELECT ID, post_title, " . join(',', $meta_fields) . " FROM $wpdb->posts ";
$request .= join(' ', $meta_joins);
$request .= " WHERE post_status='publish' AND post_type='post'";
$request .= " LIMIT $numberOf";
最好让另一个SQL自己检索元。
答案 1 :(得分:0)
不知道这是否可以帮到你,但这是一个有效的SQl查询,可以获得帖子和post_meta。
SELECT DISTINCT ID, p.post_title, p.post_status, p.post_date, m.meta_key, m.meta_value
FROM wp_posts p, wp_postmeta m
WHERE p.post_author = 2
AND p.post_status = 'publish'
AND p.post_date >= '2009-01-01'
AND p.post_date <= '2010-05-28'
AND p.ID = m.post_id
答案 2 :(得分:0)
修改查询如下:
$request = "SELECT ID, post_title, meta_value FROM $wpdb->posts p, $wpdb->postmeta pm, $wpdb->postmeta pm1";
$request .= " WHERE p.ID = pm.post_id";
$request .= " AND p.post_status='publish' AND p.post_type='post' AND pm.meta_key='_liked' AND pm1.meta_key='wbphoto';
$request .= " ORDER BY pm.meta_value+0 DESC LIMIT $numberOf";