Support » Developing with WordPress » WordPress form two queries on one submission

  • I made a form with such a code:

    <form name=”wyszukiwarka” id=”wyszukiwarka” class=”wyszukiwarka” >
    <select class=”miasto” id=”pa_miasto” name=”pa_miasto”>
    <option class=’opcja’ value=’miastoValue’>MiastoValue</option>
    ….
    </select>
    <select class=”typ-ogloszenia” id=”product_cat” name=”product_cat” required >
    <option class=’opcja’ value=’ProductCatValue’>TypValue</option>
    …..
    </select>

    <input type=”submit” value = “Szukaj” >
    </form>
    What I want it to do is:

    User chooses value from ‘pa_miasto’ and ‘product_cat’.
    User submits form.
    User gets query like this: mysite.com/category/ProductCatValue/?pa_miasto=miastoValue
    I’m using GET method right now and it’s working like this:

    when I choose only one select and submit, it goes right into proper category without any database ‘?’ query in url;
    example: mysite.com/category/ProductCatValue
    when I choose both selects and submit, it doesn’t go to any category, it just search database with ‘?’ query using both parameters (‘pa_miasto’ and ‘product_cat’);
    example: mysite.com/?product_cat=ProductCatValue&pa_miasto=miastoValue
    So the thing I want to achieve is a bit like halfway of these two actions. My question is: Is it some kind WordPress function that redirects to proper category while getting fitting argument keys or is it something else?
    The second question is, can I somehow split this query into two on one submission to get the result I want? (for example using some Javascript function).

    I hope it’s clear. I just want to reduce the amount of options in the further filtering. I can provide site url if there would be some problems with understanding.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Is it some kind WordPress function that redirects to proper category while getting fitting argument keys or is it something else?

    Yes, if the request matches an existing permalink you’ll get redirected.

    I’m not 100% on your question, are you trying to get this URL: mysite.com/category/ProductCatValue&pa_miasto=miastoValue?

    Hello, thank you for your reply. I want to get this URL:

    mysite.com/category/ProductCatValue?pa_miasto=miastoValue

    So the query will be searching products with chosen miastoValue, among chosen ProductCatValue category only.
    Is it even possible?

    Moderator bcworkz

    (@bcworkz)

    Yes it is possible! The problem is likely that whatever code is currently handling pa_miasto query vars is not picking up on additional parameters like category requests. If not already done, you should first hook the ‘query_vars’ filter and add “pa_miasto” to the passed array so that it will appear as a WP query var just like “cat” or “tag” does.

    Without knowing how pa_miasto parameters need to be utilized, it’s difficult to make a precise recommendation. If it can be translated into conventional WP_Query arguments, perhaps some sort of “meta_query” argument for example, then use the “pre_get_posts” action to get the parameter and setup the proper meta_query argument or whatever would be appropriate.

    If the pa_miasto parameter needs to be used directly in part of the SQL query, hook the appropriate filters in WP_Query::get_posts() and add the necessary query clauses. Filters like “posts_where”, “posts_join”, etc. Look through the source code for all of what’s available. If you need to modify several clauses, “posts_clauses” provides all the component clauses in one filter.

    Hello, thank you for your reply.
    I’ve tried to use “pre_get_posts” action but it doesn’t seem to be working.
    Actually i may not be able to use it properly. Can u suggest any solution for me?
    Here’s the URL of my website: http://icoturobic.reklamownia24.pl/
    Password: Eizo (for front end access only)

    The search form i’m trying to edit is right on the main page. The one with two selects and sumbit button ‘SZUKAJ’. When you choose second select only, it redirects to category page properly. When u choose both of them it doesn’t redirect to category page but takes both parameters to use in a query equally. That’s the issue i’m trying to solve.

    • This reply was modified 9 months, 1 week ago by  bcworkz. Reason: expand URL shortener, note pwd purpose
    Moderator bcworkz

    (@bcworkz)

    We’re very leery of URL shorteners and posted passwords here, so I’ve edited your last post accordingly. A few bad actors (or naiveté ) make these measures necessary. Thank you for the detailed example, it clearly illustrates the issue. Unfortunately it does little by itself for helping us solve it.

    “pre_get_posts” may not be an appropriate approach, or it may be perfect. It depends on how “pa_miasto” values are stored and how they are related to posts. Please explain this is greater detail. Are they taxonomy terms? Post meta or custom fields? Something else?

    reklamownia24

    (@reklamownia24)

    Well, they are taxonomy terms, to be more precise they are product attributes of Woocommerce products. I think that the problem may be caused by some function that checks if there’s a single key in the query, so then it can redirect to related URL. It would explain the reason it redirects only if one of the selects is selected.

    Moderator bcworkz

    (@bcworkz)

    Ah, I see. Then pa_miasto is a taxonomy and leszno or whatever attribute is assigned are terms. That explains the behavior you observe. Now that I’m understanding that aspect, I realize I don’t properly understand what results you want. It seems you still want a product category list. But how should the pa_miasto attribute selected factor in? Do you want products that are either in the selected category OR in the selected miasto, as well as those with both? Instead of what we get now, only products with both the product category AND in the selected miasto. Or is it something else?

    In any case, “pre_get_posts” should be able to do it, but I would need to know what the current query vars are in order to know how to manipulate them. Hook “pre_get_posts” and var_dump the query_vars property of the passed WP_Query object. Only var_dump if the query’s is_main_query() method returns true. This prevents distracting output from other queries. The output will be a big block of text at the top of the page, but if you look at the HTML source it will appear much more organized.

    I think both the “product_cat” and “pa_miasto” query vars will have assigned values, please confirm. Also post the value of the “tax_query” query var, which should be a rather complex multi-level array structure. Depending on what results you want, we should be able to manipulate these items into something that works for you.

    reklamownia24

    (@reklamownia24)

    I want to get a two-way scenario (note that product_cat select is required, pa_miasto select is optional):
    A. User selects only product_cat, and he goes straight to chosen category.
    B. User selects product_cat and pa_miasto and he goes to the chosen category and gets the results of pa_miasto restricted to the chosen category.

    Scenario A works super fine, that’s the way i want it to work.

    Scenario B uses AND logic which is almost fine. It gives proper results but lacks the actuall logic i want to use (product_cat –> pa_miasto). This way the sidebar on the left would not provide any filtering options to change product_cat. That’s the way i want it to work. You can observe the exact result i want to achieve by going through the A scenario and then, changing filters on pa_miasto attribute (the first dropdown on the left) so the sidebar stays the same way (without posibillity to change product_cat attribute on checkboxes) and the results given are also fine. Doing this with a form placed on the main page is the thing i want to achieve.

    These are the results given by var_dump function over the query (product_cat=’lokal’&pa_miasto=’leszno’):

    object(WP_Query)#1526 (48) {
      ["query"]=>
      array(2) {
        ["product_cat"]=>
        string(5) "lokal"
        ["pa_miasto"]=>
        string(6) "leszno"
      }
      ["query_vars"]=>
      array(60) {
        ["product_cat"]=>
        string(5) "lokal"
        ["pa_miasto"]=>
        string(6) "leszno"
        ["error"]=>
        string(0) ""
        ["m"]=>
        string(0) ""
        ["p"]=>
        int(0)
        ["post_parent"]=>
        string(0) ""
        ["subpost"]=>
        string(0) ""
        ["subpost_id"]=>
        string(0) ""
        ["attachment"]=>
        string(0) ""
        ["attachment_id"]=>
        int(0)
        ["name"]=>
        string(0) ""
        ["static"]=>
        string(0) ""
        ["pagename"]=>
        string(0) ""
        ["page_id"]=>
        int(0)
        ["second"]=>
        string(0) ""
        ["minute"]=>
        string(0) ""
        ["hour"]=>
        string(0) ""
        ["day"]=>
        int(0)
        ["monthnum"]=>
        int(0)
        ["year"]=>
        int(0)
        ["w"]=>
        int(0)
        ["category_name"]=>
        string(0) ""
        ["tag"]=>
        string(0) ""
        ["cat"]=>
        string(0) ""
        ["tag_id"]=>
        string(0) ""
        ["author"]=>
        string(0) ""
        ["author_name"]=>
        string(0) ""
        ["feed"]=>
        string(0) ""
        ["tb"]=>
        string(0) ""
        ["paged"]=>
        int(0)
        ["meta_key"]=>
        string(0) ""
        ["meta_value"]=>
        string(0) ""
        ["preview"]=>
        string(0) ""
        ["s"]=>
        string(0) ""
        ["sentence"]=>
        string(0) ""
        ["title"]=>
        string(0) ""
        ["fields"]=>
        string(0) ""
        ["menu_order"]=>
        string(0) ""
        ["embed"]=>
        string(0) ""
        ["category__in"]=>
        array(0) {
        }
        ["category__not_in"]=>
        array(0) {
        }
        ["category__and"]=>
        array(0) {
        }
        ["post__in"]=>
        array(0) {
        }
        ["post__not_in"]=>
        array(0) {
        }
        ["post_name__in"]=>
        array(0) {
        }
        ["tag__in"]=>
        array(0) {
        }
        ["tag__not_in"]=>
        array(0) {
        }
        ["tag__and"]=>
        array(0) {
        }
        ["tag_slug__in"]=>
        array(0) {
        }
        ["tag_slug__and"]=>
        array(0) {
        }
        ["post_parent__in"]=>
        array(0) {
        }
        ["post_parent__not_in"]=>
        array(0) {
        }
        ["author__in"]=>
        array(0) {
        }
        ["author__not_in"]=>
        array(0) {
        }
        ["orderby"]=>
        string(16) "menu_order title"
        ["order"]=>
        string(3) "ASC"
        ["meta_query"]=>
        array(0) {
        }
        ["tax_query"]=>
        array(2) {
          ["relation"]=>
          string(3) "AND"
          [0]=>
          array(4) {
            ["taxonomy"]=>
            string(18) "product_visibility"
            ["field"]=>
            string(16) "term_taxonomy_id"
            ["terms"]=>
            array(1) {
              [0]=>
              int(7)
            }
            ["operator"]=>
            string(6) "NOT IN"
          }
        }
        ["posts_per_page"]=>
        int(15)
        ["wc_query"]=>
        string(13) "product_query"
      }
      ["tax_query"]=>
      object(WP_Tax_Query)#10636 (6) {
        ["queries"]=>
        array(2) {
          [0]=>
          array(5) {
            ["taxonomy"]=>
            string(11) "product_cat"
            ["terms"]=>
            array(1) {
              [0]=>
              string(5) "lokal"
            }
            ["field"]=>
            string(4) "slug"
            ["operator"]=>
            string(2) "IN"
            ["include_children"]=>
            bool(true)
          }
          [1]=>
          array(5) {
            ["taxonomy"]=>
            string(9) "pa_miasto"
            ["terms"]=>
            array(1) {
              [0]=>
              string(6) "leszno"
            }
            ["field"]=>
            string(4) "slug"
            ["operator"]=>
            string(2) "IN"
            ["include_children"]=>
            bool(true)
          }
        }
        ["relation"]=>
        string(3) "AND"
        ["table_aliases":protected]=>
        array(0) {
        }
        ["queried_terms"]=>
        array(2) {
          ["product_cat"]=>
          array(2) {
            ["terms"]=>
            array(1) {
              [0]=>
              string(5) "lokal"
            }
            ["field"]=>
            string(4) "slug"
          }
          ["pa_miasto"]=>
          array(2) {
            ["terms"]=>
            array(1) {
              [0]=>
              string(6) "leszno"
            }
            ["field"]=>
            string(4) "slug"
          }
        }
        ["primary_table"]=>
        NULL
        ["primary_id_column"]=>
        NULL
      }
      ["meta_query"]=>
      bool(false)
      ["date_query"]=>
      bool(false)
      ["queried_object"]=>
      object(WP_Term)#10639 (10) {
        ["term_id"]=>
        int(70)
        ["name"]=>
        string(5) "Lokal"
        ["slug"]=>
        string(5) "lokal"
        ["term_group"]=>
        int(0)
        ["term_taxonomy_id"]=>
        int(70)
        ["taxonomy"]=>
        string(11) "product_cat"
        ["description"]=>
        string(0) ""
        ["parent"]=>
        int(0)
        ["count"]=>
        int(1)
        ["filter"]=>
        string(3) "raw"
      }
      ["queried_object_id"]=>
      int(70)
      ["post_count"]=>
      int(0)
      ["current_post"]=>
      int(-1)
      ["in_the_loop"]=>
      bool(false)
      ["comment_count"]=>
      int(0)
      ["current_comment"]=>
      int(-1)
      ["found_posts"]=>
      int(0)
      ["max_num_pages"]=>
      int(0)
      ["max_num_comment_pages"]=>
      int(0)
      ["is_single"]=>
      bool(false)
      ["is_preview"]=>
      bool(false)
      ["is_page"]=>
      bool(false)
      ["is_archive"]=>
      bool(true)
      ["is_date"]=>
      bool(false)
      ["is_year"]=>
      bool(false)
      ["is_month"]=>
      bool(false)
      ["is_day"]=>
      bool(false)
      ["is_time"]=>
      bool(false)
      ["is_author"]=>
      bool(false)
      ["is_category"]=>
      bool(false)
      ["is_tag"]=>
      bool(false)
      ["is_tax"]=>
      bool(true)
      ["is_search"]=>
      bool(false)
      ["is_feed"]=>
      bool(false)
      ["is_comment_feed"]=>
      bool(false)
      ["is_trackback"]=>
      bool(false)
      ["is_home"]=>
      bool(false)
      ["is_404"]=>
      bool(false)
      ["is_embed"]=>
      bool(false)
      ["is_paged"]=>
      bool(false)
      ["is_admin"]=>
      bool(false)
      ["is_attachment"]=>
      bool(false)
      ["is_singular"]=>
      bool(false)
      ["is_robots"]=>
      bool(false)
      ["is_posts_page"]=>
      bool(false)
      ["is_post_type_archive"]=>
      bool(false)
      ["query_vars_hash":"WP_Query":private]=>
      string(32) "a0a4689b8594fa0cac2d2e0510d78bf5"
      ["query_vars_changed":"WP_Query":private]=>
      bool(false)
      ["thumbnails_cached"]=>
      bool(false)
      ["stopwords":"WP_Query":private]=>
      NULL
      ["compat_fields":"WP_Query":private]=>
      array(2) {
        [0]=>
        string(15) "query_vars_hash"
        [1]=>
        string(18) "query_vars_changed"
      }
      ["compat_methods":"WP_Query":private]=>
      array(2) {
        [0]=>
        string(16) "init_query_flags"
        [1]=>
        string(15) "parse_tax_query"
      }
    }
    
        	

    I hope it helps. Thank you in advance.

    Moderator bcworkz

    (@bcworkz)

    Thanks for collecting the data. What is strange here is the query object contains the correct taxonomy query, but it’s not where it needs to be! The one that ends up getting used is incorrect. But this makes the solution rather simple, we can copy the correct taxonomy query to the query_vars version, overwriting the wrong one. Add this code to your pre_get_posts callback (if necessary, adjust $query to be whatever variable name is used to collect the passed query object):
    $query->set('tax_query', [ $query->tax_query->queries, 'relation'=>'AND',]);

    This also needs to be inside a conditional that checks for this particular situation, to avoid applying it to other, inappropriate situations. Scan the query data for something that’s unique about this query. Perhaps the “query” element containing both product_cat and pa_miasto elements only occurs for this situation?

    This destroys the product visibility functionality. If you are using it, we’ll need to merge it with the first array element. This is untested, but it should result in only products assigned both terms being returned. It will not however, change the sidebar behavior in any way. The side bar is a completely different issue. What widget are you using that’s not behaving as you wish?

    reklamownia24

    (@reklamownia24)

    Actually, sidebar behavior is the only thing i need to change. Sidebar filtering options are being generated by “WOOF Woocommerce Product Filter” widget.
    So you say, it could be a problem with this widget’s way of translating the query to generate filtering options?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘WordPress form two queries on one submission’ is closed to new replies.