我正在尝试进行自定义SQL查询以获取2批post_meta键和值。这是我的代码:
<?php
$queried_post = get_post($post_id);
$title = $term->name ;
$userid = $current_user->user_email;
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key='user_programme_name' AND meta_value='".$title."' AND meta_key='user_email' AND meta_value='".$userid."' ");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0 ) {
?>
<p style="width:430px;">You have already requested a place on this course</p>
<?php } ?>
当我jsut查询第一组键和值时,查询的第一部分有效:
meta_key='user_programme_name' AND meta_value='".$title."'
但是,只要我添加第二批:
AND meta_key='user_email' AND meta_value='".$userid."'
它不起作用。有什么我做错了吗?
非常感谢任何帮助。
干杯,丹
答案 0 :(得分:1)
修复SQL语法,以便在同一列上选择多个值:
<?php
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key IN ('user_programme_name', 'user_email') AND meta_value IN ('.$title.', '.$userid.') ");
?>
答案 1 :(得分:1)
使用类似的东西:
$args = array( 'post_type' => 'myposttype',
'posts_per_page' => …,
'offset' => …,
'meta_query'=>array(
'relation' => 'AND',
array(
'key'=>'_first_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
),
array(
'key'=>'_second_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
)
)
);
$yourloop = new WP_Query( $args );