1

N.B. : all my work is on wordpress.

I was working for an advanced form with 3 input text in purpose to show a list of hotels.

The problem is i tried an infinity tricks and functions but without any results, the only result i got is when i search with tag, but i have also search by adress and rating.

another problem is i used in the first time a custom taxonomy in the first way then i used metabox but nothing i can't find how to show results.

this is the form

enter image description here


i want to create a form to search for hotels by three criteria: by adresse, tag and rating so what i'm askin is what i need to do? did i need to use metabox or taxonomy and the ultime probleme is how to show search results because i tryed by taxonomies and metabox but i didn't succeed to show results.

edit

this is my search form

<form method="get" role="search" action="<?php bloginfo('url'); ?>" id="searchform">
    <input type="text" name="adr" id="adress" value="" /> 
    <span class="sepinput">_</span>
    <input type="text" name="tag" id="tag" value="" /> 
    <span class="sepinput">_</span>
    <input type="text" name="dvoi" id="rate" value="" /> 
    <span class="sepinput">_</span>
    <input type="hidden" name="post_type" value="hotels" />
    <input type="submit" id="searchsubmit" value="Recherche" />
</form>

and thats my code on search page but wasn't work on it then i have created an archive-hotels page.

                <div id="main-area">
                    <?php
$args = array(
    'post_type'  => 'hotel',
    'posts_per_page' => 3,
    'paged' => $paged,
    'relation' => 'AND',
    'meta_query' => array(
        array(
            'key'     => 'hotel[adr]',
            'value'   => $adr,
            'compare' => 'LIKE',
        ),
        array(
            'key'     => 'hotel[dvoi]',
            'value'   => $dvoi,
            'compare' => '>',
        ),
    ),
);
$wp_querysearch = new WP_Query( $args );
                    ?>
<?php if ($wp_querysearch->have_posts()) : while ($wp_querysearch->have_posts()) : $wp_querysearch->the_post(); ?>
<div class="all_hotel">
    <div class="hotel_image">
    <?php $post_id = get_the_ID();
            $hotel_data = get_post_meta( $post_id, '_hotel', true );
            if(has_post_thumbnail()) {
                $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'image-style2' );
                echo '<img src="' . $src[0] . '" alt="" />';
            } else {
                echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) .'/images/indxglri1.png" alt="" />';} ?>
        </div><!-- fin image -->
        <div class="hotel_content">
            <a href="<?php the_permalink(); ?>"><h1 class="hotel_title"><?php the_title(); ?></h1></a>
            <div class="hotel_subcontent">
                <div class="hotel_text">
                    <?php $trimmedtxt = substr(get_the_content(),0,224);
                    echo $trimmedtxt; ?>
                </div><!--end hotel_text -->
        </div>
</div>
<?php endwhile;?>
<?php else :
    echo 'Pas de hotels pour l\'instant';
endif;?>
                </div> <!-- end #main-area -->

Did I need to match the id of form or the name and with what i need to match them. because thats my metabox.

<input type="text" id="adr" value="<?php echo $adr; ?>" name="hotel[adr]" size="40" />
<input type="text" id="dvoi" value="<?php echo $dvoi; ?>" name="hotel[dvoi]" size="40" />

EDIT2

                    <?php
$voiture = $_GET['voiture'];
$adresses = $_GET['adresses'];
$tg = $_GET['tag'];
$post_id = get_the_ID();
$hotel_data = get_post_meta( $post_id, '_hotel', true );
$dvoi = ( empty( $hotel_data['dvoi'] ) ) ? '' : $hotel_data['dvoi'];
$adr = ( empty( $hotel_data['adr'] ) ) ? '' : $hotel_data['adr'];       
$args = array(
    'post_type'  => 'hotels',
    'posts_per_page' => 3,
    'relation' => 'OR',
    'meta_query' => array(
        array(
            'key'     => $adr,
            'value'   => $adresses,
            'compare' => 'LIKE',
        ),
        array(
            'key'     => 'tag',
            'value'   => $tg,
            'compare' => 'LIKE',
        ),
        array(
            'key'     => $dvoi,
            'value'   => $voiture,
            'compare' => '=',
        ),
    ),
);
$wp_querysearch = new WP_Query( $args );
                    ?>
<?php if ($wp_querysearch->have_posts()) : while ($wp_querysearch->have_posts()) : $wp_querysearch->the_post(); ?>

this is my code, but its still skeeping all showing result and its show me the else message.

<?php endwhile;?>
<?php else :
    echo 'Pas de hotels pour l\'instant';
endif;?>
  • It's not clear what you are trying to do and what problems you are facing. Please describe in details. – sakibmoon May 26 '15 at 14:13
  • thank you for your intervention, i added another explanation. – Carl Willis May 26 '15 at 14:21
  • Are the hotels custom post type? Is the tag wordpress default tag or something else? – sakibmoon May 26 '15 at 14:26
  • yes hotels are custom post type and i used just default tag and i get results for tag. – Carl Willis May 26 '15 at 14:29
  • On which page are you using the search from? On which page do you want the search result to go i.e. display the search result? – sakibmoon May 27 '15 at 19:16
2

As you've already get results with tag I'm going to give suggestion only for address and rating.

For your current situation, the best option is to use Wordpress post meta to store these values. Then you can use meta query to retrieve those posts.

$args = array(
    'post_type'  => 'hotel',
    'meta_query' => array(
        array(
            'key'     => 'address',
            'value'   => $adr,
            'compare' => 'LIKE',
        ),
    ),
);
$query = new WP_Query( $args );

For rating

$args = array(
    'post_type'  => 'hotel',
    'meta_query' => array(
        array(
            'key'     => 'rating',
            'value'   => $rating,
            'compare' => '>',
        ),
    ),
);
$query = new WP_Query( $args );

EDIT

You can combine the meta queries in a single query like this

$args = array(
    'post_type'  => 'hotel',
    'relation' => 'AND'
    'meta_query' => array(
        array(
            'key'     => 'address',
            'value'   => $adr,
            'compare' => 'LIKE',
        ),
        array(
            'key'     => 'rating',
            'value'   => $rating,
            'compare' => '>',
        ),
    ),
);
$query = new WP_Query( $args );

You can also choose OR relationship if you want between the meta queries.

whenever get_search_form() is called, the content of searchform.php is included. This is the default search page. If you want to keep that search option, you should include this code in some place else. Maybe in another file and include it where you want to display your custom search option.

That being said, your main problem is how to receive the passed values. As you are using GET method, you'd use $_GET['adr'] to receive the value after the form has been submitted. But you should change name post_type and tag to something else as they are wordpress built-in query var.

EDIT

You are saving post meta as array. Don't do that. Save them separately. Save it such that, you get address by $adr = get_post_meta($post_id, 'adr', true); and rating by $dvoi = get_post_meta($post_id, 'dvoi', true);

Your metabox code should be

<input type="text" id="adr" value="<?php echo $adr; ?>" name="adr" size="40" />
<input type="text" id="dvoi" value="<?php echo $dvoi; ?>" name="dvoi" size="40" />

Here's the meta query to display posts that have rating equal $voiture OR address like $adresses AND contain tag $tag.

$args = array(
    'tag' => $tag,
    'post_type'  => 'hotels',
    'posts_per_page' => 3,
    'relation' => 'OR',
    'meta_query' => array(
         array(
            'key'     => 'adr',
            'value'   => $adresses,
            'compare' => 'LIKE'
        ),
        array(
            'key'     => 'dvoi',
            'value'   => $voiture,
            'compare' => '='
        )
    )
);
$wp_querysearch = new WP_Query( $args );

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.