PHP搜索查询与单选按钮

时间:2015-03-18 14:29:53

标签: php mysql wordpress search

尝试在我的搜索查询中添加一个可用于绘图的新语句(单选按钮值是或否)。你能告诉我,我的思维方式是否正确。非常感谢。

此致

if ( count( $_GET ) > 0 && isset( $_GET[ 'location' ] ) && isset( $_GET[ 'bedrooms' ] ) && isset( $_GET[ 'price' ] ) && isset($_GET[ 'plot_available' ]) == 'yes' ) {

                                    $location = $_GET[ 'location' ];
                                    $bedrooms = $_GET[ 'bedrooms' ];
                                    $price = $_GET[ 'price' ];
                                    $plot_available = $GET['plot_available'];

我添加了isset($ _ GET ['plot_available']并且搜索查询无效。

更新

    <?php
                                if ( count( $_GET ) > 0 && isset( $_GET[ 'location' ] ) && isset( $_GET[ 'bedrooms' ] ) && isset( $_GET[ 'price' ] ) && isset($_GET[ 'plot_available' ]) && ($_GET['plot_available'] == 'yes') ) {

                                    $location = $_GET[ 'location' ];
                                    $bedrooms = $_GET[ 'bedrooms' ];
                                    $price = $_GET[ 'price' ];
                                    $plot_available = $GET['plot_available'];

                                    $args = array(
                                        'post_type' => 'development',
                                        'post_status' => 'publish',
                                        'posts_per_page' => '-1',
                                        'location' => $location,
                                        'bedrooms' => $bedrooms,
                                        'price' => $price
                                    );
                                    $query = new WP_Query( $args );

                                    if ( $query->found_posts < 1 ) {

                                        echo '<article><h2>No Results Found</h2>';
                                        echo '<p>No developments have been found for your search query. Please click <a href="' . get_home_url() . '">here</a> to return to the home page and try again with different search parameters.</p></article>';

                                    } else {

                                        echo '<ul id="posts" class="posts">';

                                            if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

                                                <li class="post clearfix">  
                                                    <?php
                                                        $loc_term = wp_get_post_terms( get_the_id(), 'location' );
                                                        $bedroom_term = wp_get_post_terms( get_the_id(), 'bedrooms' );
                                                        $price_term = wp_get_post_terms( get_the_id(), 'price-range' );
                                                        $house_term = wp_get_post_terms( get_the_id(), 'house-type' );

                                                        $link = get_term_link( $loc_term[0] ) . '#' . $post->post_title;

                                                        $loc_name = '';
                                                        if ( isset( $loc_term[0]->name ) ) {
                                                            $loc_name = ' - ' . $loc_term[0]->name;
                                                        }

                                                        echo '<h2><a href="' . $link . '">' . get_the_title() . $loc_name . '</a></h2>';
                                                        if ( isset( $bedroom_term[0] ) ) { echo '<p><strong>' . $bedroom_term[0]->name . '</strong></p>'; }
                                                        if ( isset( $price_term[0] ) ) { echo '<p><strong>' . $price_term[0]->name . '</strong></p>'; }
                                                        if ( isset( $house_term[0] ) ) { echo '<p><strong>' . $house_term[0]->name . '</strong></p>'; }
                                                    ?>
                                                </li>

                                        <?php endwhile; endif; wp_reset_query();

                                        echo '</ul>';

                                    }

                                } else {

                                    echo '<article><h2>Invalid Search</h2>';
                                    echo '<p>Oops, something went wrong with your search. Please click <a href="' . get_home_url() . '">here</a> to return to the home page and try again.</p></article>';

                                }
                            ?>

我必须只显示可用的地块。

2 个答案:

答案 0 :(得分:0)

替换此行:

isset($_GET[ 'plot_available' ]) == 'yes'

用这个:

isset($_GET[ 'plot_available' ]) && ($_GET['plot_available'] == 'yes')

答案 1 :(得分:0)

您是否应该使用tax_query按条款进行搜索? 据我所知,你需要一个tax_query。

试用此$args的更新版本:

$args = array(
    'post_type' => 'development',
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    'tax_query' => array(
        'relation' => 'AND', // not sure what is the case, could be OR
        array(
            'taxonomy' => 'location',
            'field' => 'slug',
            'terms' => $location
        ),
        array(
            'taxonomy' => 'bedrooms',
            'field' => 'slug',
            'terms' => $bedrooms
        ),
        array(
            'taxonomy' => 'price-range',
            'field' => 'slug',
            'terms' => $price
        ),
    )
);

我不确定$location$bedrooms$price的值,因此您可能希望保留原样,如果您使用的是slugs,或者如果您使用的话,请将其删除id&#39; s(因为默认为term_id)。