WordPress Custom Search Form (WooCommerce Product Category)





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







-2















Can anybody help?



Why is this not working?



<form role="search" method="get" action="/">
<input type="text" name="s" placeholder="Search">
<input type="hidden" name="post_type" value="product">
<input type="hidden" value="product_cat" name="the-journal-of-stained-glass" />
<input type="submit" value="Go">
</form>


It 'should' be filtering out search results that are only in the product category specified, but it's not.










share|improve this question





























    -2















    Can anybody help?



    Why is this not working?



    <form role="search" method="get" action="/">
    <input type="text" name="s" placeholder="Search">
    <input type="hidden" name="post_type" value="product">
    <input type="hidden" value="product_cat" name="the-journal-of-stained-glass" />
    <input type="submit" value="Go">
    </form>


    It 'should' be filtering out search results that are only in the product category specified, but it's not.










    share|improve this question

























      -2












      -2








      -2








      Can anybody help?



      Why is this not working?



      <form role="search" method="get" action="/">
      <input type="text" name="s" placeholder="Search">
      <input type="hidden" name="post_type" value="product">
      <input type="hidden" value="product_cat" name="the-journal-of-stained-glass" />
      <input type="submit" value="Go">
      </form>


      It 'should' be filtering out search results that are only in the product category specified, but it's not.










      share|improve this question














      Can anybody help?



      Why is this not working?



      <form role="search" method="get" action="/">
      <input type="text" name="s" placeholder="Search">
      <input type="hidden" name="post_type" value="product">
      <input type="hidden" value="product_cat" name="the-journal-of-stained-glass" />
      <input type="submit" value="Go">
      </form>


      It 'should' be filtering out search results that are only in the product category specified, but it's not.







      php wordpress woocommerce






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 13:43









      BadScooter1980BadScooter1980

      23




      23
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Hello you have mistaken in the code in the place of name in the input field you have provided value of the category and in the place of value you provided name. Please try as following.



          <form role="search" method="get" action="/">
          <input type="text" name="s" placeholder="Search">
          <input type="hidden" name="post_type" value="product">
          <input type="hidden" name="product_cat" value="the-journal-of-stained-glass" />
          <input type="submit" value="Go">
          </form>


          Please add this following in your functions.php in that please replace the taxonomy with your taxonomy



          function wc_hide_selected_terms( $terms, $taxonomies, $args ) {
          $new_terms = array();
          if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
          foreach ( $terms as $key => $term ) {
          if ( ! in_array( $term->slug, array( 'uncategorized' ) ) ) {
          $new_terms = $term;
          }
          }
          $terms = $new_terms;
          }
          return $terms;
          }
          add_filter( 'get_terms', 'wc_hide_selected_terms', 10, 3 );

          function advanced_search_query($query) {

          if($query->is_search()) {
          // category terms search.
          if (isset($_GET['product_cat']) && !empty($_GET['product_cat'])) {
          $query->set('tax_query', array(array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => $_GET['product_cat'])
          ));
          }
          }
          return $query;
          }
          add_action('pre_get_posts', 'advanced_search_query', 1000);





          share|improve this answer


























          • Hi James, thanks for that, but unfortunately that hasn't helped in fixing the problem.

            – BadScooter1980
            Nov 16 '18 at 13:55











          • Did you add coding in functions.php file in your theme ?

            – James Paul
            Nov 16 '18 at 13:56











          • Any specific coding? I have nothing relating to the search function in there.

            – BadScooter1980
            Nov 16 '18 at 13:57











          • I have added the code for functions.php also in that please replace your taxonomy with the current taxonomy.

            – James Paul
            Nov 16 '18 at 14:02











          • Thanks, but that gives me the following error Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /homepages/11/d741879078/htdocs/wp-includes/wp-db.php on line 1102

            – BadScooter1980
            Nov 16 '18 at 14:11












          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%2f53339069%2fwordpress-custom-search-form-woocommerce-product-category%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














          Hello you have mistaken in the code in the place of name in the input field you have provided value of the category and in the place of value you provided name. Please try as following.



          <form role="search" method="get" action="/">
          <input type="text" name="s" placeholder="Search">
          <input type="hidden" name="post_type" value="product">
          <input type="hidden" name="product_cat" value="the-journal-of-stained-glass" />
          <input type="submit" value="Go">
          </form>


          Please add this following in your functions.php in that please replace the taxonomy with your taxonomy



          function wc_hide_selected_terms( $terms, $taxonomies, $args ) {
          $new_terms = array();
          if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
          foreach ( $terms as $key => $term ) {
          if ( ! in_array( $term->slug, array( 'uncategorized' ) ) ) {
          $new_terms = $term;
          }
          }
          $terms = $new_terms;
          }
          return $terms;
          }
          add_filter( 'get_terms', 'wc_hide_selected_terms', 10, 3 );

          function advanced_search_query($query) {

          if($query->is_search()) {
          // category terms search.
          if (isset($_GET['product_cat']) && !empty($_GET['product_cat'])) {
          $query->set('tax_query', array(array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => $_GET['product_cat'])
          ));
          }
          }
          return $query;
          }
          add_action('pre_get_posts', 'advanced_search_query', 1000);





          share|improve this answer


























          • Hi James, thanks for that, but unfortunately that hasn't helped in fixing the problem.

            – BadScooter1980
            Nov 16 '18 at 13:55











          • Did you add coding in functions.php file in your theme ?

            – James Paul
            Nov 16 '18 at 13:56











          • Any specific coding? I have nothing relating to the search function in there.

            – BadScooter1980
            Nov 16 '18 at 13:57











          • I have added the code for functions.php also in that please replace your taxonomy with the current taxonomy.

            – James Paul
            Nov 16 '18 at 14:02











          • Thanks, but that gives me the following error Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /homepages/11/d741879078/htdocs/wp-includes/wp-db.php on line 1102

            – BadScooter1980
            Nov 16 '18 at 14:11
















          0














          Hello you have mistaken in the code in the place of name in the input field you have provided value of the category and in the place of value you provided name. Please try as following.



          <form role="search" method="get" action="/">
          <input type="text" name="s" placeholder="Search">
          <input type="hidden" name="post_type" value="product">
          <input type="hidden" name="product_cat" value="the-journal-of-stained-glass" />
          <input type="submit" value="Go">
          </form>


          Please add this following in your functions.php in that please replace the taxonomy with your taxonomy



          function wc_hide_selected_terms( $terms, $taxonomies, $args ) {
          $new_terms = array();
          if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
          foreach ( $terms as $key => $term ) {
          if ( ! in_array( $term->slug, array( 'uncategorized' ) ) ) {
          $new_terms = $term;
          }
          }
          $terms = $new_terms;
          }
          return $terms;
          }
          add_filter( 'get_terms', 'wc_hide_selected_terms', 10, 3 );

          function advanced_search_query($query) {

          if($query->is_search()) {
          // category terms search.
          if (isset($_GET['product_cat']) && !empty($_GET['product_cat'])) {
          $query->set('tax_query', array(array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => $_GET['product_cat'])
          ));
          }
          }
          return $query;
          }
          add_action('pre_get_posts', 'advanced_search_query', 1000);





          share|improve this answer


























          • Hi James, thanks for that, but unfortunately that hasn't helped in fixing the problem.

            – BadScooter1980
            Nov 16 '18 at 13:55











          • Did you add coding in functions.php file in your theme ?

            – James Paul
            Nov 16 '18 at 13:56











          • Any specific coding? I have nothing relating to the search function in there.

            – BadScooter1980
            Nov 16 '18 at 13:57











          • I have added the code for functions.php also in that please replace your taxonomy with the current taxonomy.

            – James Paul
            Nov 16 '18 at 14:02











          • Thanks, but that gives me the following error Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /homepages/11/d741879078/htdocs/wp-includes/wp-db.php on line 1102

            – BadScooter1980
            Nov 16 '18 at 14:11














          0












          0








          0







          Hello you have mistaken in the code in the place of name in the input field you have provided value of the category and in the place of value you provided name. Please try as following.



          <form role="search" method="get" action="/">
          <input type="text" name="s" placeholder="Search">
          <input type="hidden" name="post_type" value="product">
          <input type="hidden" name="product_cat" value="the-journal-of-stained-glass" />
          <input type="submit" value="Go">
          </form>


          Please add this following in your functions.php in that please replace the taxonomy with your taxonomy



          function wc_hide_selected_terms( $terms, $taxonomies, $args ) {
          $new_terms = array();
          if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
          foreach ( $terms as $key => $term ) {
          if ( ! in_array( $term->slug, array( 'uncategorized' ) ) ) {
          $new_terms = $term;
          }
          }
          $terms = $new_terms;
          }
          return $terms;
          }
          add_filter( 'get_terms', 'wc_hide_selected_terms', 10, 3 );

          function advanced_search_query($query) {

          if($query->is_search()) {
          // category terms search.
          if (isset($_GET['product_cat']) && !empty($_GET['product_cat'])) {
          $query->set('tax_query', array(array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => $_GET['product_cat'])
          ));
          }
          }
          return $query;
          }
          add_action('pre_get_posts', 'advanced_search_query', 1000);





          share|improve this answer















          Hello you have mistaken in the code in the place of name in the input field you have provided value of the category and in the place of value you provided name. Please try as following.



          <form role="search" method="get" action="/">
          <input type="text" name="s" placeholder="Search">
          <input type="hidden" name="post_type" value="product">
          <input type="hidden" name="product_cat" value="the-journal-of-stained-glass" />
          <input type="submit" value="Go">
          </form>


          Please add this following in your functions.php in that please replace the taxonomy with your taxonomy



          function wc_hide_selected_terms( $terms, $taxonomies, $args ) {
          $new_terms = array();
          if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
          foreach ( $terms as $key => $term ) {
          if ( ! in_array( $term->slug, array( 'uncategorized' ) ) ) {
          $new_terms = $term;
          }
          }
          $terms = $new_terms;
          }
          return $terms;
          }
          add_filter( 'get_terms', 'wc_hide_selected_terms', 10, 3 );

          function advanced_search_query($query) {

          if($query->is_search()) {
          // category terms search.
          if (isset($_GET['product_cat']) && !empty($_GET['product_cat'])) {
          $query->set('tax_query', array(array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => $_GET['product_cat'])
          ));
          }
          }
          return $query;
          }
          add_action('pre_get_posts', 'advanced_search_query', 1000);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 '18 at 13:51

























          answered Nov 16 '18 at 13:53









          James PaulJames Paul

          6010




          6010













          • Hi James, thanks for that, but unfortunately that hasn't helped in fixing the problem.

            – BadScooter1980
            Nov 16 '18 at 13:55











          • Did you add coding in functions.php file in your theme ?

            – James Paul
            Nov 16 '18 at 13:56











          • Any specific coding? I have nothing relating to the search function in there.

            – BadScooter1980
            Nov 16 '18 at 13:57











          • I have added the code for functions.php also in that please replace your taxonomy with the current taxonomy.

            – James Paul
            Nov 16 '18 at 14:02











          • Thanks, but that gives me the following error Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /homepages/11/d741879078/htdocs/wp-includes/wp-db.php on line 1102

            – BadScooter1980
            Nov 16 '18 at 14:11



















          • Hi James, thanks for that, but unfortunately that hasn't helped in fixing the problem.

            – BadScooter1980
            Nov 16 '18 at 13:55











          • Did you add coding in functions.php file in your theme ?

            – James Paul
            Nov 16 '18 at 13:56











          • Any specific coding? I have nothing relating to the search function in there.

            – BadScooter1980
            Nov 16 '18 at 13:57











          • I have added the code for functions.php also in that please replace your taxonomy with the current taxonomy.

            – James Paul
            Nov 16 '18 at 14:02











          • Thanks, but that gives me the following error Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /homepages/11/d741879078/htdocs/wp-includes/wp-db.php on line 1102

            – BadScooter1980
            Nov 16 '18 at 14:11

















          Hi James, thanks for that, but unfortunately that hasn't helped in fixing the problem.

          – BadScooter1980
          Nov 16 '18 at 13:55





          Hi James, thanks for that, but unfortunately that hasn't helped in fixing the problem.

          – BadScooter1980
          Nov 16 '18 at 13:55













          Did you add coding in functions.php file in your theme ?

          – James Paul
          Nov 16 '18 at 13:56





          Did you add coding in functions.php file in your theme ?

          – James Paul
          Nov 16 '18 at 13:56













          Any specific coding? I have nothing relating to the search function in there.

          – BadScooter1980
          Nov 16 '18 at 13:57





          Any specific coding? I have nothing relating to the search function in there.

          – BadScooter1980
          Nov 16 '18 at 13:57













          I have added the code for functions.php also in that please replace your taxonomy with the current taxonomy.

          – James Paul
          Nov 16 '18 at 14:02





          I have added the code for functions.php also in that please replace your taxonomy with the current taxonomy.

          – James Paul
          Nov 16 '18 at 14:02













          Thanks, but that gives me the following error Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /homepages/11/d741879078/htdocs/wp-includes/wp-db.php on line 1102

          – BadScooter1980
          Nov 16 '18 at 14:11





          Thanks, but that gives me the following error Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /homepages/11/d741879078/htdocs/wp-includes/wp-db.php on line 1102

          – BadScooter1980
          Nov 16 '18 at 14:11




















          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%2f53339069%2fwordpress-custom-search-form-woocommerce-product-category%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

          Bressuire

          Vorschmack

          Quarantine