简体   繁体   中英

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". 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.

this code is what I tried, doesn't work. $searchYachts is how I call my meta data

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;


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM