Reputation: 869
In wordpress search, I am trying to search only post type "products". However it's still showing my "journal posts". I am using a plugin called "search-everything" to allow my search to extend to some extra post features like tags, (I have tagged the word "shoes" an all shoe products and even used the word "shoes" in the product title, which is something Wordpress looks into even without any search plugins. Apart from this, the search template is working decently.
Please have a look at the website to test it: http://jadepalacecollective.com/?s=shoes
Example above is a search for "mules".
and it's also retrieving a post from the journal (the big image on the bottom)
This is my code (I have slimmed it down for readability's sake) I first go to this main search.php page
<?php
if (get_post_type() == 'product' ) : ?>
<header class="page-header">
<h1 class="page-title searchresults-for"><?php printf( esc_html__( 'Search Results for: %s', 'palace' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header><!-- .page-header -->
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'search' );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
If the search.php finds something it then fetches the template search-content.php to display some info about the product.
<?php if (get_post_type() == 'product' ) : ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</article><!-- #post-## -->
<?php endif; ?>
Would you happen to guess why this is happening? Am I doing something incorrectly?
Upvotes: 0
Views: 141
Reputation: 2775
You can limit search results for specific post types with this code in your functions.php
function search_rules($query) {
if ($query->is_search) {
$query->set('post_type', 'product');
}
return $query;
}
add_filter('pre_get_posts','search_rules');
Upvotes: 1