Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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

}


}
share|improve this answer
add comment

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);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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