• I have a custom search form. Now I’m using it to filter and get the post ID. I’d like to add in the same form other search criteria and a separate submit button. In particular I need to filter by 3 category-subcategory1-subcategory-2 select imputs, 2 taxonomy-select imputs , tags checkbox inputs. Any idea? My form is:

    function custom_search_form( $form ) {
      $form = '<form role="search" method="get" id="searchform" class="searchform" action="' . home_url( '/' ) . '" >
        <div class="custom-form"><label class="screen-reader-text" for="s">' . __( 'Search:' ) . '</label>
        <input type="text" placeholder="ricerca per codice"  value="' . get_search_query() . '" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
      </div>
      </form>';
    
      return $form;
    }
    add_filter( 'get_search_form', 'custom_search_form', 40 );

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Separate submit button means a different form as well. If you intend to keep your current search form as well, you should output the new form separately from “get_search_form”. How you do that depends on where you want this form to appear. You could define your own search form function and call it from an appropriate place on any templates you want.

    As long as one of the fields is named “s”, WP will still process the submission as a search request. You would hook the “pre_get_posts” action to alter what criteria is used in the search. Your action callback could distinguish between the two search forms by what other fields are submitted along with “s”.

    Where applicable, your fields should have the names of existing query vars, along with possible values appropriate for that query var. If that’s not possible, you can introduce your own query vars. Whitelist them through the “query_vars” filter. Your pre_get_posts callback can then transform the submitted values into query vars that WP understands. If the search criteria involves taxonomies, your callback would construct an appropriate “tax_query” array.

    When you add a “tax_query” arg to a search query, the entire tax query SQL clause is ANDed with the other WHERE criteria, so matching posts must match the search criteria AND the tax query criteria. If you wanted OR logic instead, you’d need to patch up the SQL through the “posts_request” filter. There’s no way to alter query vars to get OR logic between search and tax query criteria.

    You can easily get OR logic between the various taxonomy criteria. Search criteria by default applies OR logic between all of the searched fields, but the full search clause and full tax query clause are always ANDed together.

Viewing 1 replies (of 1 total)
  • The topic ‘How adding select imputs and checkboxes’ is closed to new replies.