Change wordpress search results from post to product












1















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.










share|improve this question



























    1















    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.










    share|improve this question

























      1












      1








      1








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 0:04









      logos_164logos_164

      190115




      190115
























          1 Answer
          1






          active

          oldest

          votes


















          0














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





          share|improve this answer
























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














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





          share|improve this answer
























          • 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
















          0














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





          share|improve this answer
























          • 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














          0












          0








          0







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





          share|improve this answer













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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word