简体   繁体   English

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

[英]WP->Query get Custom Post ID from meta_value & meta_key

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. 我有一个自定义的post_type'house',我想找到具有meta_key和某些元值的自定义post_type的post_id。

Lets say i want to find post_id for a house with meta_key='house_id' meta_value='231sd1223' 可以说我想找到带有meta_key ='house_id'meta_value ='231sd1223'的房屋的post_id

How exactly would i do that with wp->query? 我将如何使用wp-> query做到这一点?

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. 但是,查询元值会使数据库查询更多,请考虑循环遍历“房屋”帖子类型,而不是仅在meta_value等于房屋号时才执行某些操作。

// 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();
global $wpdb;
$results = $wpdb->get_results( "select post_id, meta_key from $wpdb->postmeta where meta_value = '231sd1223.'", ARRAY_A );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过在Wordpress中由meta_key和meta_value标识的自定义查询字符串来检索发布 - Retrieve Post by Custom Query String identified by meta_key and meta_value in Wordpress WP_query通过meta_key和meta_value进行过滤 - WP_query Filter through meta_key and meta_value WPDB查询-根据_meta_key返回meta_value - WPDB Query - Return meta_value based on _meta_key 构造MySQL查询(meta_key / meta_value表) - Construct MySQL query ( meta_key/meta_value table) 需要从wp_postmeta表中更新某些meta_key的meta_value,其中post_date(来自wp_posts)&lt;&#39;xxxx-xx-xx%&#39; - Need to update the meta_value for certain meta_key from wp_postmeta table where post_date (from wp_posts) < 'xxxx-xx-xx %' 在 wp_postmeta 中更新/插入 meta_key 和 meta_value - Update/insert meta_key and meta_value in wp_postmeta 从WP数据库获取Meta_Value - Get Meta_Value from WP database 通过高级自定义字段(meta_key和meta_value)获取自定义帖子时遇到问题 - Having Issues getting custom posts by Advanced Custom Fields (meta_key and meta_value) 如果 meta_key = Delivery Date 和 meta_value = this day,则在 WooCommerce 每天 23:00 将订单状态从自定义状态更新为已完成 - Update order status from custom statuses to completed in WooCommerce everyday at 23.00 if meta_key = Delivery Date and meta_value = this day Sql 查询以排除 meta_value where meta_key = x on SUM for WooCommerce - Sql query to exclude meta_value where meta_key = x on SUM for WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM