[英]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.