• I created a shortcode and put the code in my functions.php, at the moment I have to define the function I want to display:

    // function that runs when shortcode is called
    
    
    function wpb_demo_shortcode() { 
    }
    
    // register shortcode
    add_shortcode('ricerca', 'wpb_demo_shortcode');

    I’m trying to do a searchbox more or less like this:

    I’d like to start with defining the form and the first selectors, which are concatenated one with other, which could be the point of start?

    • This topic was modified 1 year, 1 month ago by James Huff. Reason: link moved to proper field

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

Viewing 11 replies - 31 through 41 (of 41 total)
  • Thread Starter sacconi

    (@sacconi)

    For the sort field I could start by assegning a variable to this field, such as

    $imput_select = $wp_query->get('order');

    this variable will be added to the $output

    Moderator bcworkz

    (@bcworkz)

    Yes, that’s a good choice. You should typically have a conditional or ternary phrase to assign a default value if you get an empty string in return. I think you might get the WP default order anyway if the query string were order='', so maybe not necessary? I’m not sure. Try it without and see what happens ??

    Thread Starter sacconi

    (@sacconi)

    I’m trying to put some pieces together, like in a puzzle..

    $imput_select = $wp_query->get('order');
    $imput_select ''== $imput_select ? 0 : $imput_select; 
    $imput_select= 
    <select name="order" onchange="myFunction()">
      <option value="?orderby='meta_value_num'&meta_key='function_prezzo_minimo'&order='DES' ">dal+al-</option>
      <option value="?orderby='meta_value_num'&meta_key='function_prezzo_minimo'&order='ASC' ">dal-al+</option>
    </select>
    
    Moderator bcworkz

    (@bcworkz)

    For the value attributes I think you just want either ‘ASC’ or ‘DESC’, not all the other query vars. The browser will build the correct query string if you use the right field names. You’ll need to use selected() again so the correct order is shown when the results are displayed.

    As for the necessary orderby and meta_key query vars, add hidden fields to the form for each. For example:
    <input type"hidden" name="orderby" value="meta_value_num">

    Thread Starter sacconi

    (@sacconi)

    This is a little bit too difficult ?? Maybe are there somewhere in the net some tracks I can follow? I havent found examples to customize

    • This reply was modified 1 year ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    I’m not sure how useful examples from elsewhere will help. You’re struggling with things that are specific to your site like the proper query string needed. The query string is built by the browser based on the form fields you develop, so you need fields specific to your situation.

    You’ve used selected() elsewhere in your code, the only difference is the values being compared.
    <option value="DES" <?php selected( $imput_select, 'DES'); ?>>dal+al-</option>
    If selected, along with “order” name for the select tag, we get the query string order=DES

    Then add hidden fields similar to the one in my previous reply. My earlier example will result in the query string orderby=meta_value_num. You also need a similar hidden field for the “meta_key” query var.

    Thread Starter sacconi

    (@sacconi)

    For the moment I would set aside the field to sort the results, because I first have to dust off a problem reported in another topic. I would like to try to do the following: show check boxes one per tag, as you can see in the search box of search and filter https://test.sacconicase.com/. Since tags are a default wordpress taxonomy, perhaps it is easier to get to the result

    Moderator bcworkz

    (@bcworkz)

    I think focusing on one challenge at a time is a good idea.

    If you let the browser build GET args from check boxes, you will not get the expected results. You’ll need to pre-process the data into a format that WP recognizes. There are different ways to do this. I recommend using method="POST" as a form attribute. I’d also use the form’s action attribute to send the request to a specific WP page.

    The specific destination page has a custom template associated with it that contains the pre-processing code. The code constructs valid args for instantiating a custom WP_Query object. The object is then used to output the results in loop.

    Thread Starter sacconi

    (@sacconi)

    This goal is also at a standstill because after deleting the search and filter plugin my custom search box also disappeared. I opened a specific topic about it but unfortunately without success

    Thread Starter sacconi

    (@sacconi)

    Inside the above shortcode I’d like to re-use a search criterium that I have in my current site. Thies selector lets the user filter the apartments accondingly to their lodging capacity (number of persons). I have a selector until 16 persons. Every option is for a “number+” (when I click on 5+ I display all the apartment for 5 people OR MORE). Here is what I have:

    <div class="fila">
                    <label><? echo getwID(302); //n.Persone?>:</label>
                    <? echo getw('da')?>
                    <select name="adv_min_pers" class="campo02">
                   	<?  for ($i =1; $i <= 16; $i++) print '<option value="'.$i.'">'.$i.'</option>'; ?>
                    </select>
                    <? echo getw('a')?>
                    <select name="adv_max_pers" class="campo02">
    			<?  for ($i =1; $i <= 16; $i++) printf('<option value="%s" %s>%s</option>',$i,$i == 4 ? 'selected' : '', $i); ?>
                    </select>
                  </div>
                  <div class="fila">
    

    The meta key that rapresents the field with the value for “number of people” is “function_ospiti”

    Moderator bcworkz

    (@bcworkz)

    If your search criteria involves more than one meta value, you’ll need to use the “meta_query” arg of WP_Query to specify what criteria to query for. It’s usage is kind of complex, but it’s also quite flexible and powerful. It’s likely you can define any combination of meta data criteria you might need. The challenge will be in formatting it correctly so the query returns the desired results. For meta_query documentation, find the “Custom Field” item in the WP_Query doc page’s table of contents. Follow the link. I cannot deep link directly to it from here. When I try, the landing scroll position is incorrect.

    Another reason to use POST instead of GET method is it’s more suitable for forms with lots of field data. In PHP, instead of getting the passed form data from $_GET you’ll need to use $_POST or $_REQUEST.

    The reason I suggest the form’s action attribute takes one to a specific page that’s based on a custom template is it’ll make developing the right form handling code easier. To start with, the template could do:
    `echo ‘

    ';
    var_dump( $_POST );
    echo '

    ‘;
    This way you can see exactly what sort of data is coming from the form. You can remove this code once the search form is working correctly. Your task for this template will be to alter the sent data into a format that works for WP_Query.

    With the right args supplied to WP_Query, your template can output the search results using the usual WP loop that outputs posts.

Viewing 11 replies - 31 through 41 (of 41 total)
  • The topic ‘Shortcode for runnung a search-box’ is closed to new replies.