[英]Search pages using custom fields
I'm stuck with this problem for a week already. 我已经在这个问题上停留了一周。 I'd like to create a search bar that will show a list of pages with the data filled in the custom fields.
我想创建一个搜索栏,将显示一个页面列表,其中的数据已填充到自定义字段中。 For example, if I select "Pre Owned" as status the search bar, it should show the pages with the custom field where the meta data is "Pre Owned".
例如,如果我选择“ Pre Owned”作为状态栏,它应该显示带有自定义字段的页面,其中元数据是“ Pre Owned”。 I have absolutely no idea how to do this.
我完全不知道该怎么做。 I know it's possible to search in custom posts, but I want it in pages.
我知道可以在自定义帖子中进行搜索,但我希望在页面中进行搜索。 http://jaroyachting.com/dev/yacht-list/ is what the list looks like.
列表的外观为http://jaroyachting.com/dev/yacht-list/ 。
this code is what I tried, doesn't work. 这段代码是我尝试过的,不起作用。 $searchYachts is how I call my meta data
$ 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
Thanks in advance! 提前致谢!
Here in this meta query you can pass the meta values, 在此元查询中,您可以传递元值,
$args = array(
'meta_query' => array(
array(
'key' => 'cp_annonceur',
'value' => 'professionnel',
'compare' => '=',
)
)
);
$query = new WP_Query($args);
This is as far as I got. 据我所知。 But now it shows every page...
但是现在它显示了每个页面...
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.