我正在尝试编写一个查询,该查询将查找并显示与我的输入具有相同自定义字段值的所有帖子。
在wordpress中我有以下内容......
我的查询是......
$pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_value = 'petrol' OR wpostmeta.meta_value = 'local' ORDER BY wpostmeta.meta_value DESC", OBJECT);
如果我删除'或wpostmeta.meta_value ='local'这样可以正常工作,并将自定义字段值作为'汽油'拉出正确的帖子
有人可以告诉我哪里出错吗?目前它显示我的所有帖子,甚至是那些已经删除的帖子,它也循环显示了很多次......
答案 0 :(得分:1)
尝试:
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND (wpostmeta.meta_value = 'petrol' OR wpostmeta.meta_value = 'local')
ORDER BY wpostmeta.meta_value DESC"
答案 1 :(得分:0)
想出来......
<?php
$customkey = 'Network'; // set to your custom key
$customvalue = 'Local'; // set to custom value
global $wpdb;
$my_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->postmeta WHERE ID = $wpdb->postmeta.post_id AND meta_key = '$customkey' AND meta_value = '$customvalue' ORDER BY post_date DESC");
foreach ($my_posts as $post) :
setup_postdata($post);
echo '<div><a href="';
the_permalink();
echo '"></div>';
the_title();
endforeach;
?>