dongye9071 2016-11-17 10:20 采纳率: 0%
浏览 112
已采纳

WP->查询从meta_value和meta_key获取自定义帖子ID

I have been trying for a couple of hours to make this work - but for some reason its simply to difficult for me. I have a custom post_type 'house', and I want to find the post_id of my custom post_type with a meta_key and certain meta value.

Lets say i want to find post_id for a house with meta_key='house_id' meta_value='231sd1223'

How exactly would i do that with wp->query?

  • 写回答

2条回答 默认 最新

  • douhui4831 2016-11-17 10:29
    关注

    Here you have the query even with a loop. However, querying meta values is making more DB queries, consider looping throught "house" post type and than doing something only if meta_value is equal the house number.

    // WP_Query arguments
    $args = array (
        'post_type'              => array( 'house' ),
        'post_status'            => array( 'publish' ),
        'meta_query'             => array(
            array(
                'key'       => 'house_id',
                'value'     => '231sd1223',
            ),
        ),
    );
    
    // The Query
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            // do something
        }
    } else {
        // no posts found
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置