gpt4 book ai didi

php - Wordpress SQL 选择多个元值/元键/自定义字段

转载 作者:行者123 更新时间:2023-11-29 06:22:19 27 4
gpt4 key购买 nike

我正在尝试修改 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 和$照片 = wb照片

这是我的 Phpmyadmin 的屏幕截图 alt text

最佳答案

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 来自己检索元。

关于php - Wordpress SQL 选择多个元值/元键/自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2531806/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com