网址变量不会返回结果

时间:2017-12-20 17:39:50

标签: php html wordpress

我在我的网站上的几个区域使用自定义WP查询,并且使用最近的WordPress 4.9.1更新,我的WP查询部分已停止运行。我知道有一种解决这个问题的方法,不涉及回滚到WordPress 4.8,但我个人不熟悉php或WP Query,知道如何解决它。

我遇到的问题不是最初加载页面时,而是使用位置,类别和字母的查询变量时。每次使用这些变量运行查询时,都不会返回任何结果。

或者完美地运行关键字查询或最大发布查询功能。

我很感激能够在下面的代码中找到问题的人的帮助。

这是我在WP Query中使用的代码:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;



$category = get_query_var( 'nabc', false );
$location = get_query_var( 'nabl', false );
$keywords = get_query_var( 'nabk', false );
$alphabet = get_query_var( 'naba', false );
$maxposts = get_query_var( 'showall', 10 );

$meta_query = array(
    'relation' => 'AND',
);

if( $category && strtoupper( $category ) != "AFFILIATE OR REGION" ) {
    // Category / Type search
    $meta_query[] = array(
        'key'       => 'directory_fields_%_region_or_affiliate',
        'value'     => $category,
        'compare'   => '='
     );
}

if( $location && strtoupper( $location ) != "STATE/PROVINCE" ) {
    // State/Location
    $meta_query[] = array(
        'key'       => 'directory_fields_%_state',
        'value'     => $location,
        'compare'   => '='
    );
}

if( $keywords ) {
    // keyword search
    $meta_query[] = array(
        'value'     => $keywords,
        'compare'   => 'LIKE'
    );
    // d( $meta_query );
}

if( $alphabet ) {
    // Search by Letter
     $meta_query[] = array(
         'key' => 'directory_fields_%_entity_name',
         'value' => $alphabet,
         'compare' => 'BETWEEN'
     );
 }

 // Get all the categories
 $categories = array();

 global $wpdb;

 $list = $wpdb->get_results( 'SELECT meta_value FROM ' . $wpdb-
 >postmeta . ' WHERE meta_key LIKE 
 "directory_fields_%_region_or_affiliate" GROUP BY meta_value' );

 foreach( $list as $l )
{
    if( $l->meta_value )
        $categories[] = $l->meta_value;

}

// Get all the States
$states = array();

$list = $wpdb->get_results( 'SELECT meta_value FROM ' . $wpdb-
>postmeta . ' WHERE meta_key LIKE "directory_fields_%_state" GROUP BY 
meta_value' );

foreach( $list as $l )
{
    if( $l->meta_value )
        $states[] = $l->meta_value;

}

?>

<?php get_header(); ?>


<?php $heroImage = get_field( 'hero_image' ); ?>
<div class="jumbotron" style="background: url( <?php echo 
!empty($heroImage['url']) ? $heroImage['url'] : get_field( 
'nab_directory_hero_image', 'options' );  ?> ) center center no-
repeat;background-size: cover;" class="hero-image">
    <h1>Directory</h1>
</div>


<!-- START FRONT CONTENT -->
<div id="content" style="position:relative;">

    <div class="row mobile-padding">
        <div class="container jobs-container">
            <div class="col-xs-24">
                <div class="filters container-fluid hidden-print">

                    <form class="form-inline row" action="/directory/" method="GET">
                        <div class="form-group col-sm-6">
                            <input class="form-control" type="text" name="nabk" value="<?php echo esc_html( $keywords ); ?>" placeholder="KEYWORDS">
                        </div>
                        <div class="form-group col-sm-6 col-sm-offset-1">
                            <select class="form-control" name="nabc">
                                <option>AFFILIATE OR REGION</option>
                                <?php foreach( $categories as $cat ): ?>
                                    <option <?php echo $category == $cat ? 'selected' : '' ?>><?= $cat ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group col-sm-6 col-sm-offset-1">
                            <select class="form-control" name="nabl">
                                <option>STATE/PROVINCE</option>
                                <?php foreach( $states as $state ): ?>
                                    <option <?php echo $location == $state ? 'selected' : '' ?>><?= $state ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group col-sm-4 text-right">
                            <button class="nab-btn nab-btn-blue job-filter-btn" type="submit">SEARCH</button>
                        </div>
                    </form>
                </div>
                <div class="alphabet-filters hidden-print">
                    <ul>
                        <?php for( $i = 'A'; $i != 'AA'; $i++ ): $j = $i  ?>
                        <li><a href="?naba=<?= $i . "," . ++$j ?>"><?= $i ?></a></li>
                        <?php endfor; ?>
                    </ul>
                </div>
                <?php
                    global $wp_query;
                    $wp_query = new WP_Query( array( 
                        'post_type'         => 'directory',
                        'posts_per_page'    => $maxposts,
                        'paged'             => $paged, // 104 current last page
                        'meta_query'        => $meta_query,
                        'orderby'           => 'title',
                        'order'             => 'ASC'
                    ) );
                    // query_posts( array( 
                    //     'post_type'         => 'directory',
                    //     'posts_per_page'    => $maxposts,
                    //     'paged'             => $paged,
                    //     'meta_query'        => $meta_query,
                    //     'orderby'           => 'title',
                    //     'order'             => 'ASC'
                    // ) );
                ?>
                <?php if( $wp_query->have_posts() ): ?>
                <div class="table-responsive">
                    <table id="directory-listing" class="table">
                        <thead>
                            <tr>
                                <th>CHURCH/ORGANIZATION</th>
                                <th>STATE/PROVINCE</th>
                                <th>NAME</th>
                            </tr>
                        </thead>
                        <tbody>

                            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); global $post; ?>
                                <?php if( have_rows( 'directory_fields', $post->id ) ): ?>
                                    <?php while ( have_rows( 'directory_fields', $post->id ) ) : the_row(); ?>
                                        <?php if( get_row_layout() == 'directory_details' ): ?>

                                        <tr>
                                            <th scope="row"><a href="<?= get_the_permalink(); ?>"><?= get_sub_field( 'entity_name' ); ?></a><?= get_sub_field( 'street_address_1' ) ?><p><?= get_sub_field('street_address_2') ?></p><p><?= trim(get_sub_field( 'city' )) ?></p><p><?= get_sub_field( 'business_phone' ); ?></p></th>
                                        <td><i class="fa fa-map-marker" aria-hidden="true">&nbsp;</i><?= get_sub_field( 'state' ); ?></td>
                                        <td><a href="<?= get_the_permalink(); ?>"><?= get_sub_field( 'person_name' ); ?></a><p><?= get_sub_field( 'person_title' ); ?></p></td>
                                        </tr>

                                        <?php endif; ?>
                                    <?php endwhile; ?>
                                <?php endif; ?>
                            <?php endwhile;?>

                        </tbody>
                    </table>
                </div> <!-- /.table-responsive -->
            </div>
            <nav class="page-navigation hidden-print" aria-label="Page navigation">
                <?php nab_bootstrap_pagination(); ?>
            </nav>
            <div class="directory-navbar hidden-print">
                <a href="?showall=-1&nabc=<?= $category ?>&nabk=<?= $keywords ?>&nabl=<?= $location ?>">
                    <button class="nab-btn nab-btn-blue">Show All</button>
                </a>
                <a href="javascript:window.print()">
                <button class="nab-btn nab-btn-blue">Print</button>
                </a>
            </div>
            <?php else: ?>
                <p class="text-center">No Results Found</p>
            <?php endif; ?>
            <?php wp_reset_query(); ?>
        </div> <!-- /.jobs-container -->
    </div><!-- end .row -->
</div><!-- end #content -->

很抱歉有完整的代码页,但老实说,我不知道此代码中的问题所在。

1 个答案:

答案 0 :(得分:0)

对于有类似问题的其他人来说,修复实际上很简单。

出于某种原因,WordPress 4.9.1将SQL数据库更改为使用“0”字符而不是“%”字符。这意味着任何使用“%”的查询现在都必须使用“0”。

对我而言,这意味着从以下方式切换:

if( $category && strtoupper( $category ) != "AFFILIATE OR REGION" ) {
     // Category / Type search
     $meta_query[] = array(
         'key'       => 'directory_fields_%_region_or_affiliate',
         'value'     => $category,
         'compare'   => '='
     );
} .   

为:

if( $category && strtoupper( $category ) != "AFFILIATE OR REGION" ) {
     // Category / Type search
     $meta_query[] = array(
         'key'       => 'directory_fields_0_region_or_affiliate',
         'value'     => $category,
         'compare'   => '='
     );
} .   

希望这对某人有帮助,但您的里程可能会有所不同。