Search by category displaying wrong items

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

At the moment, if you search just using a keyword it shows the correct member. But if you just search using a category it just shows the full list. What code can I wrap around the member display to show the necessary member(s)?

Read More

This is in functions.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;
}

This is in the template:

<?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(); ?>

Related posts

Leave a Reply

1 comment

  1. I don’t typically do what you are doing the way you are doing it – but the glaring thing to me is that your setting your tax query with the associative key of 'tax_query1', which as far as I know shouldn’t work. It should simply be called 'tax_query'

    Try this:

    $query1->set('tax_query', $tax_query1  );
    

    Hope that helps.