admin管理员组文章数量:1316329
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(); ?>">
<section>
<?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>
</section>
</a>
</div>
<?php endwhile; else: ?>
<p>Sorry, no products matched your search criteria.</p>
<?php endif; ?>
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(); ?>">
<section>
<?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>
</section>
</a>
</div>
<?php endwhile; else: ?>
<p>Sorry, no products matched your search criteria.</p>
<?php endif; ?>
Share
Improve this question
edited Dec 12, 2013 at 14:39
Matthew Haigh
asked Dec 12, 2013 at 13:07
Matthew HaighMatthew Haigh
114 bronze badges
2 Answers
Reset to default 1Add this line -
<?php $search_query['post_type'] = 'products'; ?>
Just before the line -
<?php $search = new WP_Query($search_query); ?>
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'
.
本文标签: Search using WPQuery
版权声明:本文标题:Search using WP_Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742003857a2411560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论