0

我正在尝试执行自定义 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 } ?>

当我查询第一组键和值时,查询的第一部分起作用:

meta_key='user_programme_name' AND meta_value='".$title."'

但是一旦我添加第二批:

AND meta_key='user_email' AND meta_value='".$userid."'

它不起作用。有什么我做错了吗?

任何帮助将不胜感激。

干杯,丹

4

2 回答 2

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.') ");

?>
于 2012-11-13T11:16:48.230 回答
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 );
于 2012-11-13T12:17:22.907 回答