2016-11-09 101 views
0

我被這個問題困住了一個星期了。 我想創建一個搜索欄,顯示填充在自定義字段中的數據的頁面列表。 例如,如果我選擇「Pre Owned」作爲搜索欄的狀態,它應該顯示包含元數據爲「Pre Owned」的自定義字段的頁面。我完全不知道如何做到這一點。我知道可以在自定義帖子中進行搜索,但我希望在頁面中進行搜索。 http://jaroyachting.com/dev/yacht-list/是列表的樣子。使用自定義字段搜索頁面

這段代碼是我試過的,不起作用。 $ searchYachts是我叫我元數據

if(isset($_POST['filter'])) { 

global $wp_query; // get the global object 
$searchYachts = get_post_meta($page->ID, 'yachtinfo', true); 
$thesearch = get_search_query(); // get the string searched 

// merge them with one or several meta_queries to meet your demand 
$args = array_merge($wp_query->query, array( 
    'meta_query' => array(
    array(
     'key' => $searchYachts["status"], 
     'value' => $_POST['status'], 
     'compare' => 'IN' 
    ) 
) 
    )); 
query_posts($args); // alter the main query to include your custom parameters 

提前感謝!

回答

0

在這裏,在這個薈萃查詢您可以通過元值,

$args = array(
    'meta_query' => array(
     array(
      'key' => 'cp_annonceur', 
      'value' => 'professionnel', 
      'compare' => '=', 
     ) 
    ) 
); 
$query = new WP_Query($args); 
+0

嘿謝謝你的幫助,但可悲的是這也不起作用。如果(isset($ _POST ['filter'])){ $ searchYachts = get_post_meta($ page-> ID,'yachtinfo',true);這將意味着我的查詢將是: if $ status = $ _POST ['status']; global $ wp_query; //獲取全局對象 的$ args =陣列( 'meta_query'=>數組( 陣列( '鍵'=> $ searchYachts [ '狀態'], '值'=> '$新', ' compare'=>'=', ) ) ); $ query = new WP_Query($ args); } – Casper

+0

鍵應該是一個字符串的元值,並在值中傳遞來自表單的post變量@Casper –

+0

我也試過了 – Casper

0

這是據我得到了。但現在它顯示每一頁...

if(isset($_POST['filter'])) { 
$status = $_POST['status']; 
$args = array(
    'meta_query' => array(
     array(
     'key' => 'status', 
     'value' => '$status', 
     'compare' => '=', 
     ) 
    ) 
); 
$property_query = new WP_Query($args); 

?>     <div id="pages"> 
<?php $pages = get_pages(array($property_query)); ?> 
<ul style="list-style:none;"> 
    <?php foreach ($pages as $page): ?> 
     <div id="schip"> <li> 
      <div id="fotoSchip"><?php echo '<a href="' . get_page_uri($page)  .'">' . get_the_post_thumbnail($page->ID, array(365, 230)) . '</div> 
<div id="infoSchip"><h5>' . $page->post_title; ?></h5></a> 
     <?php 
     $yacht = get_post_meta($page->ID, 'yachtinfo', true); 
          foreach($yacht as $list){ 
           echo '<div id="statusSchip">' .  $list['status'] . '</div>'; 
            echo '<div id="prijsSchip">Price: ' .  $list['price'] . '</div>'; 
           } 

endforeach; 


}