Wordpress - 带有帖子ID的元查询

时间:2016-02-27 14:43:17

标签: php wordpress

我可以将ID添加到$args数组吗?我需要检查特定帖子是否存在键值对自定义字段。现在它检查任何帖子中是否存在键值对。或者我是否需要执行查询,然后在返回的数组中检查我的值?

    $args = array(
     'post_type' => 'post',
     'post_status' => 'publish',
     'ID' => $_POST['post_id'],
     'meta_query' => array(
             array(
                     'key' => 'claim',
                     'value' => $user_ID
             )
     )
    );

    // perform the query
    $query = new WP_Query( $args );
    $vid_ids = $query->posts;

    if ( empty( $vid_ids ) ) {
         add_post_meta( $_POST['post_id'], 'claim', $user_ID );
    }else{
        echo "sorry";
    }

2 个答案:

答案 0 :(得分:0)

请参阅WP_Query()的post/page parameters的Codex条目。 您可以使用此

传递单个帖子ID
npm run test && node server.js

如果您想传递多个帖子ID,请使用

$query = new WP_Query( array( 'p' => 7 ) );

答案 1 :(得分:0)

使用get_post_meta获取对象的自定义字段值。

$claims=get_post_meta($ID,'claim');
$exists=false;    
if(count($claims)>0)
{
    foreach($claims as $claim)   
    {
        if($claim==$user_ID)
        {
            $exists=true;
            break;
        }
    } 
}
if(!$exists)
{
    add_post_meta( $_POST['post_id'], 'claim', $user_ID );
}
相关问题