I'm using the following WP_Query within my search.php template in order to only search the 'products' custom post type. For some reason, its returning other post types with some search terms.

Any Ideas where I'm going wrong?

<?php global $query_string; ?>

<?php $query_args = explode("&", $query_string); ?>

<?php $search_query = array( 'post_type' => 'products', 'posts_per_page' => 12, 'paged' => $paged, 'orderby' => 'name', 'order' => 'ASC' ); ?>

<?php foreach($query_args as $key => $string) {

    $query_split = explode("=", $string);

    $search_query[$query_split[0]] = urldecode($query_split[1]);

} ?>

<?php $search = new WP_Query($search_query); ?>

<?php if ( $search->have_posts() ) : while ( $search->have_posts() ) : $search->the_post(); ?>

<div class="product">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">

            <?php if( get_field('product_image') ) { ?>

                <?php $product_image = get_field('product_image'); ?>
                <img src="<?php echo $product_image['sizes']['thumbnail']; ?>" alt="<?php echo $product_image   ['alt']; ?>" >

            <?php } else { ?>

                <img src="<?php bloginfo('template_url'); ?>/images/fallback-image.png" alt="Product image coming soon.">

            <?php } ?>

            <h2><?php trimmed_title('...', 22); ?><br /><span><?php the_field('reference_number'); ?></span></h2>


<?php endwhile; else: ?>

<p>Sorry, no products matched your search criteria.</p>

<?php endif; ?> 

Add this line -

<?php $search_query['post_type'] = 'products'; ?>

Just before the line -

<?php $search = new WP_Query($search_query); ?>
  • Still getting the problem, it only seems to apply to searches that are less than 3 characters in length. So for example when searching for animals "an" returns all the unwanted results and "ani" returns the correct results? Dec 12 '13 at 13:14
  • There are stopwords that are stripped from search keyword automatically. - about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www
    – Shazzad
    Dec 12 '13 at 13:18
  • In this case then it would seem the majority of which are currently working. Why wolud this be? Dec 12 '13 at 13:22
  • It seems that the issue is happening with all terms after all. So it must be something wrong with the WP_Query. Dec 12 '13 at 14:41

Is your CPT registered as "products" or "product"? Usually the singular is used for the type name, and then labels are given for the singular & plural names. Try changing 'post_type' => 'products' to 'post_type' => 'product'.

  • Its registered as "products", gave it a try but it seemed to remove the "products" post type and leave the unwanted results? Dec 12 '13 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.