2016-11-17 70 views
0

我一直在嘗試幾個小時來完成這項工作 - 但由於某種原因,它對我來說簡直難以實現。我有一個自定義的post_type'house',我想用meta_key和特定的meta值找到我的自定義post_type的post_id。WP->查詢從meta_value和meta_key獲取自定義發佈ID

可以說,我想找到一所房子POST_ID與 meta_key =「house_id」 meta_value =「231sd1223」

我究竟該怎麼做與WP->查詢?

+0

歡迎堆棧溢出!你似乎在要求某人爲你寫一些代碼。堆棧溢出是一個問答網站,而不是代碼寫入服務。請[see here](http://stackoverflow.com/help/how-to-ask)學習如何編寫有效的問題。 – secelite

回答

1

在這裏你甚至有一個循環查詢。然而,查詢元值會進行更多的數據庫查詢,考慮循環遍歷「房子」後期類型,並且只有在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(); 
相關問題