我已经尝试了几个小时,使这个工作-但由于某种原因,它只是对我来说很难。我有一个自定义post_type 'house',我想找到我的自定义post_type的post_id与一个meta_key和一定的元值。
假设我想找到post_id的房子与meta_key = ' house_id 'sd1223 meta_value = ' 231 '
我该如何与wp->查询?
这里的查询甚至带有一个循环。然而,查询元值需要进行更多的DB查询,考虑遍历"house"post类型,而不是只在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 );