按类别搜索显示错误的项目

时间:2012-05-02 作者:Rob

http://www.mediwales.com/v3/members/

目前,如果只使用关键字进行搜索,它会显示正确的成员。但如果你只是使用一个类别进行搜索,它只会显示完整的列表。我可以在成员显示周围使用什么代码来显示所需的成员?

这是在函数中。php

add_filter(\'pre_get_posts\',\'my_filter_the_members\',10,1);
function my_filter_the_members($query1){

    //If the query is a search AND taxonomy terms are set, filter by those terms:
    if(isset($_GET[\'mw-filter-terms\'])){
        //Get array of slugs of checked terms
        $terms1 = (array) $_GET[\'mw-filter-terms\'];

        //Tax_query array
        $tax_query1 = array(array(
                    \'taxonomy\' => \'members\',
                    \'field\' => \'slug\',
                    \'terms\' => $terms1,
                    \'operator\' => \'AND\',
                )); 

        //Tell the query to filter by tax
        $query1->set(\'tax_query1\', $tax_query1  );
    }
    return $query1;
}
这在模板中:

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 */

get_header();
?>

        <!-- Main Content -->

        <div class="post" id="post-<?php the_ID(); ?>">
            <div class="entry">

                <div class="left-search">
                    <div class="page-title-search">
                        <h2>Search</h2>
                        <form id="custom-search" action="<?php bloginfo(\'url\'); ?>/" method="get">
                        <?php //Get all (non-empty) terms for taxonomy \'news-category\'
                        $args = array(\'orderby\' => \'name\',\'order\' => \'ASC\');
                        $categories = get_terms( \'members\', $args );
                        ?>
                        <!-- Visible input for search term -->
                        <p style="margin-bottom:5px!IMPORTANT;"><b>Keyword</b></p>
                        <input type="text" class="keyword" name="s" value="" />
                        <div class="clear"></div>

                        <p style="margin-top:20px!IMPORTANT;margin-bottom:2px!IMPORTANT;"><b>Specialisms</b></p>
                        <?php //Display checkbox for each term
                        $counter = 1;
                        foreach ($categories as $category) {
                            echo \'<input type="checkbox" id="field-\'.$counter.\'" class="mycheckbox" name="mw-filter-terms[]" value="\'.$category->slug.\'">\';
                            echo \'<label for="field-\'.$counter.\'" class="mycheckbox-label">\'.esc_html($category->name).\'</label>\';
                        $counter++; 
                        } ?>

                        <!-- Hidden input to set post type to news-->
                        <input type="hidden" name="post_type" value="members" />

                        <div class="clear"></div>
                        <!-- Submit button -->
                        <button class="blue medium awesome awesomeforward awesomesearch" type="submit">Search</button>
                        </form>                     


                    </div>
                </div>

                <div class="news-content" style="background-color:#ececec!IMPORTANT;">
                    <div class="page-title-content">
                        <h2>Members Directory</h2>
                    </div>
                    <div class="news-content-inner">
                        <div class="inner-holder">
                            <?php the_field(\'content\', 1886); ?>
                        </div>
                    </div>
                            <a class="blue medium awesome awesomeforward" style="margin-left:193px;margin-bottom:10px;color: white !important; " href="<?php bloginfo(\'url\'); ?>/member-signup/">Become A Member</a>                    

                <div class="news-content" style="background-color:#ececec!IMPORTANT;">
                    <div class="page-title-content">
                        <h2>Search Results</h2>
                    </div>
                    <div class="news-content-inner">
                        <?php $portfolioloop1 = new WP_Query( array( \'paged\' => get_query_var(\'paged\'), \'order\' => \'ASC\', \'orderby\' => \'title\', \'post_status\' => \'publish\', \'post_type\' => \'members\', \'posts_per_page\' => 300 ) ); ?>
                        <?php while ( $portfolioloop1->have_posts() ) : $portfolioloop1->the_post(); ?>
                        <div <?php if (get_field(\'logo\') != "") { ?>style="height:120px;"<?php } ?> class="news-item" onclick="location.href=\'<?php echo the_permalink(); ?>\'">
                            <?php if (get_field(\'logo\') != "") { ?>
                                <div style="height:110px;float:left;">
                                <table>
                                    <tr>
                                    <td height="110">
                                    <img style="margin-right:15px;" src="<?php echo the_field(\'logo\'); ?>" width="150" alt="<?php echo the_title(); ?>" />
                                    </td>
                                    </tr>
                                </table>
                                </div>
                            <?php } ?>
                            <div <?php if (get_field(\'logo\') != "") { ?>style="float:left;width:379px;"<?php } ?>>
                                <h2><a style="color:#AA3CA7!important;" href="<?php echo the_permalink(); ?>"><?php echo the_title(); ?></a></h2>
                                <p class="news-page">
                                    <?php $description = get_field(\'description\');
                                    echo substr($description,0,300) . "..." ?>
                                </p>
                            </div>  
                        </div>
                        <?php endwhile; // end of the loop. ?>

                    </div>
                <div class="clear"></div>                   
            </div>
        </div>    

<?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:eddiemoya 整理而成

我通常不会像现在这样做,但对我来说,最引人注目的是,您可以使用关联键\'tax_query1\', 据我所知,这是行不通的。应该简单地称之为\'tax_query\'

尝试以下操作:

$query1->set(\'tax_query\', $tax_query1  );
希望这有帮助。

结束

相关推荐