0

I'm building a hotel search engine and I've first created a custom content type "hotel" with Types & Views Toolset. I'm using "Search everything" plugin to query custom fields and custom taxonomy from this custom content and I've excluded the other content types from functions.php.

For now, I have these custom fields :

title address zipcode city ...

I want to exclude "address" and "title" because some addresses or hotel names contain city names and I just want results from city queries.

Is it possible to exclude fields or to only include the city field ?

Thank very much

1

If I understand you correctly, you need to have post metas in your query. This is where the meta_query parameter of the WP_Query class comes to help. The meta_query is an array of arrays.

Example from the codex:

$args = array(
   'post_type' => 'my_custom_post_type',
   'meta_key' => 'age',
   'orderby' => 'meta_value_num',
   'order' => 'ASC',
   'meta_query' => array(
       array(
           'key' => 'age',
           'value' => array(3, 4),
           'compare' => 'IN',
       )
   )
 );
 $query = new WP_Query($args);
0

If you like to query directly in $wpdb you could do it like this in search.php:

global $wp_query;
global $wpdb;
global $post; 
$search = sanitize_text_field( $_GET['s']);

$querystr = " 
    SELECT   wposts.ID, wpostmeta. *
    FROM $wpdb->posts AS wposts
            LEFT JOIN $wpdb->postmeta wpostmeta ON (wposts.ID = wpostmeta.post_id)yy
    WHERE wposts.post_status = 'publish' // all published
    AND wposts.ID NOT IN ('186','184' ,'108', '124', '78', '80', '289') // exclude all these IDs
        AND wpostmeta.meta_key= '_txt_en' // Here the customField 
        AND wpostmeta.meta_value LIKE '%".$search."%'

     GROUP BY wposts.ID 
    ";





$postids = $wpdb->get_results($querystr,OBJECT);

if ($postids){ 
    foreach ($postids as $post){
        setup_postdata($post);

        echo apply_filters('the_content', get_post_meta($post->ID, '_txt_en', true));
    }

}else{
    echo "<p>Sorry, no matches.";

}


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.