Change wordpress search results from post to product
I'm building a custom search page for my new site to include custom sections for both blog posts and product, but as a "bandaid" solution for the present, I want to just return the search results for products and not posts.
I'm using Wordpress v - 4.9.8 and using a Divi Child Theme (Divi Parent Theme v - 3.17.6)
I'm currently trying to hook into pre_get_posts with the following code in functions.php, but it's still only returning the "posts" results and not as products:
// LIMIT SEARCH TO POSTS OR PRODUCTS ONLY
add_filter('pre_get_posts','SearchFilter', 9);
function SearchFilter($query) {
if ( !is_admin() && $query->is_search ) {
$query->set('post_type', 'product');
}
return $query;
}
Original Code Doc
Any ideas on how to make this work? I am thinking maybe making my own search.php page might work instead? I feel like something simple in functions.php would work better though.
php wordpress woocommerce hook-woocommerce
add a comment |
I'm building a custom search page for my new site to include custom sections for both blog posts and product, but as a "bandaid" solution for the present, I want to just return the search results for products and not posts.
I'm using Wordpress v - 4.9.8 and using a Divi Child Theme (Divi Parent Theme v - 3.17.6)
I'm currently trying to hook into pre_get_posts with the following code in functions.php, but it's still only returning the "posts" results and not as products:
// LIMIT SEARCH TO POSTS OR PRODUCTS ONLY
add_filter('pre_get_posts','SearchFilter', 9);
function SearchFilter($query) {
if ( !is_admin() && $query->is_search ) {
$query->set('post_type', 'product');
}
return $query;
}
Original Code Doc
Any ideas on how to make this work? I am thinking maybe making my own search.php page might work instead? I feel like something simple in functions.php would work better though.
php wordpress woocommerce hook-woocommerce
add a comment |
I'm building a custom search page for my new site to include custom sections for both blog posts and product, but as a "bandaid" solution for the present, I want to just return the search results for products and not posts.
I'm using Wordpress v - 4.9.8 and using a Divi Child Theme (Divi Parent Theme v - 3.17.6)
I'm currently trying to hook into pre_get_posts with the following code in functions.php, but it's still only returning the "posts" results and not as products:
// LIMIT SEARCH TO POSTS OR PRODUCTS ONLY
add_filter('pre_get_posts','SearchFilter', 9);
function SearchFilter($query) {
if ( !is_admin() && $query->is_search ) {
$query->set('post_type', 'product');
}
return $query;
}
Original Code Doc
Any ideas on how to make this work? I am thinking maybe making my own search.php page might work instead? I feel like something simple in functions.php would work better though.
php wordpress woocommerce hook-woocommerce
I'm building a custom search page for my new site to include custom sections for both blog posts and product, but as a "bandaid" solution for the present, I want to just return the search results for products and not posts.
I'm using Wordpress v - 4.9.8 and using a Divi Child Theme (Divi Parent Theme v - 3.17.6)
I'm currently trying to hook into pre_get_posts with the following code in functions.php, but it's still only returning the "posts" results and not as products:
// LIMIT SEARCH TO POSTS OR PRODUCTS ONLY
add_filter('pre_get_posts','SearchFilter', 9);
function SearchFilter($query) {
if ( !is_admin() && $query->is_search ) {
$query->set('post_type', 'product');
}
return $query;
}
Original Code Doc
Any ideas on how to make this work? I am thinking maybe making my own search.php page might work instead? I feel like something simple in functions.php would work better though.
php wordpress woocommerce hook-woocommerce
php wordpress woocommerce hook-woocommerce
asked Nov 19 '18 at 0:04
logos_164logos_164
190115
190115
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have to follow below process to search by products
Step - 1
Search form for Custom Post Type: Products
---> Add Below code where you are representing your search form
<div>
<h3>Search Products</h3>
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search Products"/>
<input type="hidden" name="post_type" value="products" /> <!-- // hidden 'products' value -->
<input type="submit" alt="Search" value="Search" />
</form>
</div>
Step - 2
----> Add below code in your active themes function.php
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'products' )
{
return locate_template('archive-search.php'); // redirect to archive-search.php
}
return $template;
}
add_filter('template_include', 'template_chooser');
Step- 3
---> Create search result template for custom post type ( archive-search.php )
<?php
/* Template Name: Custom Search */
get_header(); ?>
<div class="contentarea">
<div id="content" class="content_right">
<h3>Search Result for : <?php echo "$s"; ?> </h3>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="posts">
<article>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<p><?php the_exerpt(); ?></p>
<p align="right"><a href="<?php the_permalink(); ?>">Read More</a></p>
<span class="post-meta"> Post By <?php the_author(); ?>
| Date : <?php echo date('j F Y'); ?></span>
</article><!-- #post -->
</div>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- content -->
</div><!-- contentarea -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
yeah that didn't do anything, search bar still appeared the same way and searching for products still reveals things in blog search.
– logos_164
Nov 22 '18 at 2:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366726%2fchange-wordpress-search-results-from-post-to-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to follow below process to search by products
Step - 1
Search form for Custom Post Type: Products
---> Add Below code where you are representing your search form
<div>
<h3>Search Products</h3>
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search Products"/>
<input type="hidden" name="post_type" value="products" /> <!-- // hidden 'products' value -->
<input type="submit" alt="Search" value="Search" />
</form>
</div>
Step - 2
----> Add below code in your active themes function.php
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'products' )
{
return locate_template('archive-search.php'); // redirect to archive-search.php
}
return $template;
}
add_filter('template_include', 'template_chooser');
Step- 3
---> Create search result template for custom post type ( archive-search.php )
<?php
/* Template Name: Custom Search */
get_header(); ?>
<div class="contentarea">
<div id="content" class="content_right">
<h3>Search Result for : <?php echo "$s"; ?> </h3>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="posts">
<article>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<p><?php the_exerpt(); ?></p>
<p align="right"><a href="<?php the_permalink(); ?>">Read More</a></p>
<span class="post-meta"> Post By <?php the_author(); ?>
| Date : <?php echo date('j F Y'); ?></span>
</article><!-- #post -->
</div>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- content -->
</div><!-- contentarea -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
yeah that didn't do anything, search bar still appeared the same way and searching for products still reveals things in blog search.
– logos_164
Nov 22 '18 at 2:38
add a comment |
You have to follow below process to search by products
Step - 1
Search form for Custom Post Type: Products
---> Add Below code where you are representing your search form
<div>
<h3>Search Products</h3>
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search Products"/>
<input type="hidden" name="post_type" value="products" /> <!-- // hidden 'products' value -->
<input type="submit" alt="Search" value="Search" />
</form>
</div>
Step - 2
----> Add below code in your active themes function.php
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'products' )
{
return locate_template('archive-search.php'); // redirect to archive-search.php
}
return $template;
}
add_filter('template_include', 'template_chooser');
Step- 3
---> Create search result template for custom post type ( archive-search.php )
<?php
/* Template Name: Custom Search */
get_header(); ?>
<div class="contentarea">
<div id="content" class="content_right">
<h3>Search Result for : <?php echo "$s"; ?> </h3>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="posts">
<article>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<p><?php the_exerpt(); ?></p>
<p align="right"><a href="<?php the_permalink(); ?>">Read More</a></p>
<span class="post-meta"> Post By <?php the_author(); ?>
| Date : <?php echo date('j F Y'); ?></span>
</article><!-- #post -->
</div>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- content -->
</div><!-- contentarea -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
yeah that didn't do anything, search bar still appeared the same way and searching for products still reveals things in blog search.
– logos_164
Nov 22 '18 at 2:38
add a comment |
You have to follow below process to search by products
Step - 1
Search form for Custom Post Type: Products
---> Add Below code where you are representing your search form
<div>
<h3>Search Products</h3>
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search Products"/>
<input type="hidden" name="post_type" value="products" /> <!-- // hidden 'products' value -->
<input type="submit" alt="Search" value="Search" />
</form>
</div>
Step - 2
----> Add below code in your active themes function.php
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'products' )
{
return locate_template('archive-search.php'); // redirect to archive-search.php
}
return $template;
}
add_filter('template_include', 'template_chooser');
Step- 3
---> Create search result template for custom post type ( archive-search.php )
<?php
/* Template Name: Custom Search */
get_header(); ?>
<div class="contentarea">
<div id="content" class="content_right">
<h3>Search Result for : <?php echo "$s"; ?> </h3>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="posts">
<article>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<p><?php the_exerpt(); ?></p>
<p align="right"><a href="<?php the_permalink(); ?>">Read More</a></p>
<span class="post-meta"> Post By <?php the_author(); ?>
| Date : <?php echo date('j F Y'); ?></span>
</article><!-- #post -->
</div>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- content -->
</div><!-- contentarea -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You have to follow below process to search by products
Step - 1
Search form for Custom Post Type: Products
---> Add Below code where you are representing your search form
<div>
<h3>Search Products</h3>
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search Products"/>
<input type="hidden" name="post_type" value="products" /> <!-- // hidden 'products' value -->
<input type="submit" alt="Search" value="Search" />
</form>
</div>
Step - 2
----> Add below code in your active themes function.php
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'products' )
{
return locate_template('archive-search.php'); // redirect to archive-search.php
}
return $template;
}
add_filter('template_include', 'template_chooser');
Step- 3
---> Create search result template for custom post type ( archive-search.php )
<?php
/* Template Name: Custom Search */
get_header(); ?>
<div class="contentarea">
<div id="content" class="content_right">
<h3>Search Result for : <?php echo "$s"; ?> </h3>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="posts">
<article>
<h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
<p><?php the_exerpt(); ?></p>
<p align="right"><a href="<?php the_permalink(); ?>">Read More</a></p>
<span class="post-meta"> Post By <?php the_author(); ?>
| Date : <?php echo date('j F Y'); ?></span>
</article><!-- #post -->
</div>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- content -->
</div><!-- contentarea -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
answered Nov 19 '18 at 5:57
raju_ewwraju_eww
644315
644315
yeah that didn't do anything, search bar still appeared the same way and searching for products still reveals things in blog search.
– logos_164
Nov 22 '18 at 2:38
add a comment |
yeah that didn't do anything, search bar still appeared the same way and searching for products still reveals things in blog search.
– logos_164
Nov 22 '18 at 2:38
yeah that didn't do anything, search bar still appeared the same way and searching for products still reveals things in blog search.
– logos_164
Nov 22 '18 at 2:38
yeah that didn't do anything, search bar still appeared the same way and searching for products still reveals things in blog search.
– logos_164
Nov 22 '18 at 2:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366726%2fchange-wordpress-search-results-from-post-to-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown