Change WordPress search to produce WooCommerce search results only





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I would like:



http://www.gadgetgogo.co.uk/?s=ipod



to return as:



http://www.gadgetgogo.co.uk/?s=ipod&post_type=product



So when using searches (slider banner and default WordPress search) it produces the second URL.










share|improve this question































    0















    I would like:



    http://www.gadgetgogo.co.uk/?s=ipod



    to return as:



    http://www.gadgetgogo.co.uk/?s=ipod&post_type=product



    So when using searches (slider banner and default WordPress search) it produces the second URL.










    share|improve this question



























      0












      0








      0








      I would like:



      http://www.gadgetgogo.co.uk/?s=ipod



      to return as:



      http://www.gadgetgogo.co.uk/?s=ipod&post_type=product



      So when using searches (slider banner and default WordPress search) it produces the second URL.










      share|improve this question
















      I would like:



      http://www.gadgetgogo.co.uk/?s=ipod



      to return as:



      http://www.gadgetgogo.co.uk/?s=ipod&post_type=product



      So when using searches (slider banner and default WordPress search) it produces the second URL.







      php wordpress woocommerce






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 9:05







      Zak

















      asked Jan 3 at 23:34









      ZakZak

      85




      85
























          2 Answers
          2






          active

          oldest

          votes


















          0














          This can be done using pre_get_posts filter. Add below code in your theme's functions.php file



              add_filter( 'pre_get_posts', 'search_by_product_only' );

          function search_by_product_only( $query ) {

          // check if search query only
          if ( $query->is_search ) {
          $query->set( 'post_type', array( 'product') ); // here you can add multiple post types in whcih you want to search
          }

          return $query;
          }





          share|improve this answer


























          • Thanks for your answer dipmala, unfortunately I can't get this to work either. Am I just dropping this into functions.php?

            – Zak
            Jan 4 at 7:18











          • Yes correct and it should working because its tested code

            – dipmala
            Jan 4 at 7:41











          • Apologies dipmala it does work in one respect but not exactly how I want it to (I should reframe my question). I already have the search only showing products but I need the URL to change. For some reason when using the Avada theme it shows two different page varieties for these two search results (you can see this in the example links above), is there a way to change the URL?

            – Zak
            Jan 4 at 9:04











          • Kindly share the code or screenshot how you use search form ?

            – dipmala
            Jan 4 at 10:28











          • Here is the code - gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… And these screenshots show the difference in style when using the WooCommerce search widget and the default WordPress search widget (WooCommerce search is the desired look for all searches) gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & To follow... Thanks so much dipmala.

            – Zak
            Jan 4 at 13:43





















          -1














          Just add this line to top of search.php



          $_GET['post_type'] = 'product'





          share|improve this answer
























          • Thanks for your reply aidinMC, unfortunately I can't get it to work by placing it on the top line. Is there a particular section I should add it to?

            – Zak
            Jan 4 at 5:13














          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%2f54031265%2fchange-wordpress-search-to-produce-woocommerce-search-results-only%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          This can be done using pre_get_posts filter. Add below code in your theme's functions.php file



              add_filter( 'pre_get_posts', 'search_by_product_only' );

          function search_by_product_only( $query ) {

          // check if search query only
          if ( $query->is_search ) {
          $query->set( 'post_type', array( 'product') ); // here you can add multiple post types in whcih you want to search
          }

          return $query;
          }





          share|improve this answer


























          • Thanks for your answer dipmala, unfortunately I can't get this to work either. Am I just dropping this into functions.php?

            – Zak
            Jan 4 at 7:18











          • Yes correct and it should working because its tested code

            – dipmala
            Jan 4 at 7:41











          • Apologies dipmala it does work in one respect but not exactly how I want it to (I should reframe my question). I already have the search only showing products but I need the URL to change. For some reason when using the Avada theme it shows two different page varieties for these two search results (you can see this in the example links above), is there a way to change the URL?

            – Zak
            Jan 4 at 9:04











          • Kindly share the code or screenshot how you use search form ?

            – dipmala
            Jan 4 at 10:28











          • Here is the code - gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… And these screenshots show the difference in style when using the WooCommerce search widget and the default WordPress search widget (WooCommerce search is the desired look for all searches) gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & To follow... Thanks so much dipmala.

            – Zak
            Jan 4 at 13:43


















          0














          This can be done using pre_get_posts filter. Add below code in your theme's functions.php file



              add_filter( 'pre_get_posts', 'search_by_product_only' );

          function search_by_product_only( $query ) {

          // check if search query only
          if ( $query->is_search ) {
          $query->set( 'post_type', array( 'product') ); // here you can add multiple post types in whcih you want to search
          }

          return $query;
          }





          share|improve this answer


























          • Thanks for your answer dipmala, unfortunately I can't get this to work either. Am I just dropping this into functions.php?

            – Zak
            Jan 4 at 7:18











          • Yes correct and it should working because its tested code

            – dipmala
            Jan 4 at 7:41











          • Apologies dipmala it does work in one respect but not exactly how I want it to (I should reframe my question). I already have the search only showing products but I need the URL to change. For some reason when using the Avada theme it shows two different page varieties for these two search results (you can see this in the example links above), is there a way to change the URL?

            – Zak
            Jan 4 at 9:04











          • Kindly share the code or screenshot how you use search form ?

            – dipmala
            Jan 4 at 10:28











          • Here is the code - gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… And these screenshots show the difference in style when using the WooCommerce search widget and the default WordPress search widget (WooCommerce search is the desired look for all searches) gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & To follow... Thanks so much dipmala.

            – Zak
            Jan 4 at 13:43
















          0












          0








          0







          This can be done using pre_get_posts filter. Add below code in your theme's functions.php file



              add_filter( 'pre_get_posts', 'search_by_product_only' );

          function search_by_product_only( $query ) {

          // check if search query only
          if ( $query->is_search ) {
          $query->set( 'post_type', array( 'product') ); // here you can add multiple post types in whcih you want to search
          }

          return $query;
          }





          share|improve this answer















          This can be done using pre_get_posts filter. Add below code in your theme's functions.php file



              add_filter( 'pre_get_posts', 'search_by_product_only' );

          function search_by_product_only( $query ) {

          // check if search query only
          if ( $query->is_search ) {
          $query->set( 'post_type', array( 'product') ); // here you can add multiple post types in whcih you want to search
          }

          return $query;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 4 at 7:41

























          answered Jan 4 at 7:02









          dipmaladipmala

          1,63211217




          1,63211217













          • Thanks for your answer dipmala, unfortunately I can't get this to work either. Am I just dropping this into functions.php?

            – Zak
            Jan 4 at 7:18











          • Yes correct and it should working because its tested code

            – dipmala
            Jan 4 at 7:41











          • Apologies dipmala it does work in one respect but not exactly how I want it to (I should reframe my question). I already have the search only showing products but I need the URL to change. For some reason when using the Avada theme it shows two different page varieties for these two search results (you can see this in the example links above), is there a way to change the URL?

            – Zak
            Jan 4 at 9:04











          • Kindly share the code or screenshot how you use search form ?

            – dipmala
            Jan 4 at 10:28











          • Here is the code - gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… And these screenshots show the difference in style when using the WooCommerce search widget and the default WordPress search widget (WooCommerce search is the desired look for all searches) gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & To follow... Thanks so much dipmala.

            – Zak
            Jan 4 at 13:43





















          • Thanks for your answer dipmala, unfortunately I can't get this to work either. Am I just dropping this into functions.php?

            – Zak
            Jan 4 at 7:18











          • Yes correct and it should working because its tested code

            – dipmala
            Jan 4 at 7:41











          • Apologies dipmala it does work in one respect but not exactly how I want it to (I should reframe my question). I already have the search only showing products but I need the URL to change. For some reason when using the Avada theme it shows two different page varieties for these two search results (you can see this in the example links above), is there a way to change the URL?

            – Zak
            Jan 4 at 9:04











          • Kindly share the code or screenshot how you use search form ?

            – dipmala
            Jan 4 at 10:28











          • Here is the code - gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… And these screenshots show the difference in style when using the WooCommerce search widget and the default WordPress search widget (WooCommerce search is the desired look for all searches) gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & To follow... Thanks so much dipmala.

            – Zak
            Jan 4 at 13:43



















          Thanks for your answer dipmala, unfortunately I can't get this to work either. Am I just dropping this into functions.php?

          – Zak
          Jan 4 at 7:18





          Thanks for your answer dipmala, unfortunately I can't get this to work either. Am I just dropping this into functions.php?

          – Zak
          Jan 4 at 7:18













          Yes correct and it should working because its tested code

          – dipmala
          Jan 4 at 7:41





          Yes correct and it should working because its tested code

          – dipmala
          Jan 4 at 7:41













          Apologies dipmala it does work in one respect but not exactly how I want it to (I should reframe my question). I already have the search only showing products but I need the URL to change. For some reason when using the Avada theme it shows two different page varieties for these two search results (you can see this in the example links above), is there a way to change the URL?

          – Zak
          Jan 4 at 9:04





          Apologies dipmala it does work in one respect but not exactly how I want it to (I should reframe my question). I already have the search only showing products but I need the URL to change. For some reason when using the Avada theme it shows two different page varieties for these two search results (you can see this in the example links above), is there a way to change the URL?

          – Zak
          Jan 4 at 9:04













          Kindly share the code or screenshot how you use search form ?

          – dipmala
          Jan 4 at 10:28





          Kindly share the code or screenshot how you use search form ?

          – dipmala
          Jan 4 at 10:28













          Here is the code - gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… And these screenshots show the difference in style when using the WooCommerce search widget and the default WordPress search widget (WooCommerce search is the desired look for all searches) gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & To follow... Thanks so much dipmala.

          – Zak
          Jan 4 at 13:43







          Here is the code - gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… And these screenshots show the difference in style when using the WooCommerce search widget and the default WordPress search widget (WooCommerce search is the desired look for all searches) gallery.mailchimp.com/ade690fc170d975f78dac3981/images/… & To follow... Thanks so much dipmala.

          – Zak
          Jan 4 at 13:43















          -1














          Just add this line to top of search.php



          $_GET['post_type'] = 'product'





          share|improve this answer
























          • Thanks for your reply aidinMC, unfortunately I can't get it to work by placing it on the top line. Is there a particular section I should add it to?

            – Zak
            Jan 4 at 5:13


















          -1














          Just add this line to top of search.php



          $_GET['post_type'] = 'product'





          share|improve this answer
























          • Thanks for your reply aidinMC, unfortunately I can't get it to work by placing it on the top line. Is there a particular section I should add it to?

            – Zak
            Jan 4 at 5:13
















          -1












          -1








          -1







          Just add this line to top of search.php



          $_GET['post_type'] = 'product'





          share|improve this answer













          Just add this line to top of search.php



          $_GET['post_type'] = 'product'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 0:35









          aidinMCaidinMC

          1,04421225




          1,04421225













          • Thanks for your reply aidinMC, unfortunately I can't get it to work by placing it on the top line. Is there a particular section I should add it to?

            – Zak
            Jan 4 at 5:13





















          • Thanks for your reply aidinMC, unfortunately I can't get it to work by placing it on the top line. Is there a particular section I should add it to?

            – Zak
            Jan 4 at 5:13



















          Thanks for your reply aidinMC, unfortunately I can't get it to work by placing it on the top line. Is there a particular section I should add it to?

          – Zak
          Jan 4 at 5:13







          Thanks for your reply aidinMC, unfortunately I can't get it to work by placing it on the top line. Is there a particular section I should add it to?

          – Zak
          Jan 4 at 5:13




















          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%2f54031265%2fchange-wordpress-search-to-produce-woocommerce-search-results-only%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas