【问题标题】:PHP search query with radio button带有单选按钮的 PHP 搜索查询
【发布时间】:2015-03-18 14:29:52
【问题描述】:

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

问候

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>';

                                }
                            ?>

我必须只显示可用的图。

【问题讨论】:

  • 你错过了'_'(下划线)......
  • $GET != $_GET。而isset(...) == 'yes' 没用。 isset 返回布尔值 true/false,而不是字符串。

标签: php mysql wordpress search


【解决方案1】:

替换这一行:

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

用这个:

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

【讨论】:

  • 可以把html表单代码贴在这里吗?也许你错过了什么
  • 这是一个wordpress网站
【解决方案2】:

您不应该使用 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 的值,因此如果您使用 slug,您可能希望保留原样,或者如果您使用 id 则将其删除(因为默认值是term_id)。

【讨论】:

  • 搜索查询工作正常,但我想在搜索查询中添加语句 plot_available。因此,如果我们有可用的房产,其搜索条件如卧室价格和位置,那么房产就会显示出来。 Plot_available 是一个单选按钮,其值为 yes 或 no
  • 我不确定。我正在使用自定义字段。我已将 plot_available 设置为单选按钮。
猜你喜欢
  • 1970-01-01
  • 2012-06-19
  • 2017-04-21
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
相关资源
最近更新 更多