2016-11-09 104 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; 


}