Note: it is no longer necessary to use a custom template to control which fields are in the search dropdown. There is an attribute in the list shortcode “search_fields” where you can determine which fields are in the dropdown, so setting it up to search on a single field is as simple as:
[pdb_list search_fields=city ]
With that, the search dropdown won’t be shown and all searches will now take place on the “city” field.
You can also use the attribute to set a list of fields to include in the dropdown.
In some situations, you want your users to find a record by searching on only one field. For example, a property lookup where the users is expected to know the parcel number, or some other situation where you only want users searching on a single field.
This is very easy to implement, you’ll need to create a custom template for your list shortcode. Check this page for the general instructions on how to do that. Use the “pdb-list-detailed.php” template as your starting point.
In that template is a function that produces the field selector dropdown. What you need to do is replace that function with a hidden field that determines the field the user will be searching on.
In the pdb-list-detailed template, you’ll find:
<?php <?php /* * there are 4 options for this function which defines which fields will be * available in the search dropdown selector: * 1. "all fields" text: set the text of the default "all fields" item: * leave it "false" to use the internationalized default * 2. print it or return the element as a value: only use this if you * need to alter the HTML directly. You will need to print the item for * it to be seen. If 'true', the function prints the selector dropdown. * 3. columns: supply an array of column names if you want to define the * list of fields that can be used for searching: 'false' uses all displayed * fields * 4. sorting: you can choose to sort the list by 'column' (the order they * appear in the table), 'alpha' (alphabetical order), or 'order' which * uses the defined group/field order */ $this->column_selector( false, true, false, 'column' ); ?> ?>
We just need to replace this whole block of code (most of it is just an explanation how to use the $this->column selector method) with our hidden field. Notice that we are no longer inside of the <?php ?> tags, this is straight HTML:
<input type="hidden" name="search_field" value="your_search_field_name" />
You will replace “your_search_field_name” with the name of the field you want your users to search on. That’s it!
Here is the complete template, demonstrating how to set this up for the “city” field. You can use this template as a starting point if you want.