Posted on by

Customizing the List Search: Searching on Only One Field

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:

First NameLast NameClub RoleCityStateMain ActivityPhoto
Oscar oscar
David Valladares
Sharfa Sharafi
gabri mendez
test test
Vijay Jacob
gianni gianni
ak sun
Olivia Samlal Madison Indiana
Dom test

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:

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:

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.

<?php
/*
this template demonstrates how to set up a simple search that only searches on a single field: city
to change which field is searched on, look at line 50, change the "value" of that hidden field
*/
?>
<div class="wrap <?php echo $this->wrap_class ?>" id="<?php echo $this->list_anchor ?>">
<?php
/*
* SEARCH/SORT FORM
*
* the search/sort form is ony presented when enabled in the shortcode.
* It's also skipped when refreshing the list after a sort or filter operation
*
*/
if ( $filter_mode != 'none' && ! $filtering ) : ?>
<?php echo $this->search_error_style ?>
<div class="pdb-searchform">
<?php
/* this element is where error and feedback messages are shown to the user.
* It is only visible if there is a message
*
* don't change the classnames or IDs, they are requted for the error messagees to show up properly
* do change the strings in the "_e" functions for your message preference
*/?>
<div class="pdb-error pdb-search-error" style="display:none">
<p class="search_field_error"><?php _e( 'Please select a column to search in.', 'participants-database' )?></p>
<p class="value_error"><?php _e( 'Please type in something to search for.', 'participants-database' )?></p>
</div>
<?php
/* this method prints the top of the search/sort form
*
* if you want to put the search on a different page, you can specify the
* target page in this method, and the search results will be shown on that
* page
*/
$this->search_sort_form_top(); ?>
<?php if ( $filter_mode == 'filter' || $filter_mode == 'both' ) : ?>
<fieldset class="widefat">
<legend><?php _e('Search', 'participants-database' )?>:</legend>
<?php // change the value of this field to set which field is searched ?>
<input type="hidden" name="search_field" value="city" />
<input name="operator" type="hidden" class="search-item" value="<?php echo ( Participants_Db::plugin_setting_is_true( 'strict_search' ) ? '=' : 'LIKE' ) ?>" />
<?php
/*
* the following text input is where the user types in the search term
* if you want to add a placeholder value, add it as a placeholder attribute to the tag
*
* you can also replace this with a dropdown or other type of input
*/
?>
<input id="participant_search_term" type="text" name="value" class="search-item" value="<?php echo $this->list_query->current_filter('search_term') ?>">
<?php
/*
* this method places the submit buttons
*
* you can optionally set the button text for each one by supplying them
* in a config array...for example:
* $values = array( 'submit' => 'Search Records', 'clear' => 'Clear the Search Parameters' );
*/
$this->print_search_submit_buttons(); ?>
</fieldset>
<?php endif ?>
<?php if ( $filter_mode == 'sort' || $filter_mode == 'both' ) : ?>
<fieldset class="widefat">
<legend><?php _e('Sort by', 'participants-database' )?>:</legend>
<?php
/*
* this function sets the fields in the sorting dropdown. It has two options:
* 1. columns: an array of field names to show in the sorting dropdown. If
* 'false' shows default list of sortable fields as defined
* 2. 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->set_sortables(false, 'column');
?>
<?php $this->sort_form() ?>
</fieldset>
<?php endif ?>
</form>
</div>
<?php endif ?>
<?php
/* END SEARCH/SORT FORM */
/* LIST DISPLAY */
/*
* NOTE: the container for the list itself (excluding search and pagination
* controls) must have a class of "list-container" for AJAX search/sort to
* function
*/
?>
<table class="wp-list-table widefat fixed pages list-container" >
<?php
/* print the count if enabled in the shortcode
*
* the tag wrapping the count statment can be supplied in the function argument, example here
*/
$this->print_list_count('<caption class="list-display-count %s">');
?>
<?php if ( $record_count > 0 ) : ?>
<thead>
<tr>
<?php /*
* this function prints headers for all the fields
* replacement codes:
* %2$s is the form element type identifier
* %1$s is the title of the field
*/
$this->print_header_row( '<th class="%2$s" scope="col">%1$s</th>' );
?>
</tr>
</thead>
<?php // print the table footer row if there is a long list
if ( $records_per_page > 30 ) : ?>
<tfoot>
<tr>
<?php $this->print_header_row( '<th class="%2$s" scope="col">%1$s</th>' ) ?>
</tr>
</tfoot>
<?php endif ?>
<tbody>
<?php while ( $this->have_records() ) : $this->the_record(); // each record is one row ?>
<tr>
<?php while( $this->have_fields() ) : $this->the_field(); // each field is one cell ?>
<td>
<?php echo PDb_FormElement::get_field_value_display($this->field); ?>
</td>
<?php endwhile; // fields ?>
</tr>
<?php endwhile; // records ?>
</tbody>
<?php else : // if there are no records ?>
<tbody>
<tr>
<td><?php if ($this->is_search_result === true) echo Participants_Db::$plugin_options['no_records_message'] ?></td>
</tr>
</tbody>
<?php endif; // $record_count > 0 ?>
</table>
<?php $this->show_pagination_control(); ?>
</div>

14 thoughts on “Customizing the List Search: Searching on Only One Field

  1. Hi!

    Is it possible with that system to allow a search on all fields, without forcing the choice of a specific field in the dropdown list? And if yes, is it possible to allow both systems : Search in all fields or a specific one.

    Thx for your help.

    1. This is possible using the Combo Multisearch add-on.

  2. Just a brief note…
    I was struggling with your search_fields shortcode and eventually worked out that I needed to put the fields in quotes e.g. for the situation / fields that I’m using:
    [pdb_list search_fields="resource_title, resource_notes"]

    1. Yes, any time there are spaces in the attribute value, quotes must be used. I’m sorry that wasn’t made clear.

  3. Hi Roland, great work on the plugin! it has come a long way.
    I want to know if i can do away with the search drop down list but have the search form search multiple fields. like first name and last name concurrently .

    1. You’ll need to get the Combo Multisearch add-on to do that.

      1. can i use the plugin for diff sub domains which is technically extensions of the parent site.?

  4. Hi, I’m still getting this set up, and it seems great. However I have a search problem: When I run a search it gives results. If I have to click to the second or other pages of results, it displays all database results again instead of those that are just search results. Just wondering what is going on here:

    https://growgreatfruitprogram.com/fruit-tree-database/

    Thanks.

    1. Hugh, I just tested, and it looks like it’s working fine. You could be having trouble with your cache. If you’re using any kind of page cache (not browser cache!) you should disable it for your search page.

  5. Thank you. Yes, it is working for me now also. Before I was not getting results. Thank you.

  6. My search function is not working on this page:

    http://www.fsacrylics.com/acrylicpro/

    I am interested in the upgrade to search that you offer but want to see this work first.

    Any ideas?

    1. Hi Jon,

      Can you be more specific? It tested fine for me, but I don’t know what you are expecting.

  7. Thanks – your plugin is fantastic. I am having an issue though – when I use the [pdb_list] I get the results correctly and if I click for the next set of records that also works. However if I include any search code on my page I only get the spinning wheel to the right. My database is also not being sorted by last_name like I have it configured to do or there is something wrong in the database on multiple records though in looking at the records I cannot determine what they are.
    The link is http://clubisland.org/home/?page_id=5810
    Email me private if you need the password to access this page.

    1. Hi David,

      You’re getting a “forbidden” error on the ajax call. Probably a security plugin or perhaps security setting on your site. You can solve this either by turning off AJAX (in the plugin settings under the advanced tab) or a more technical solution involves finding out what security code it getting triggered and setting up an exception so the search and sort can submit successfully. This will affect pagination of the list as well.

      I do see what looks like an alphabetical sort by last name in the list.

Leave a Reply to Jon Cancel reply

Your email address will not be published. Required fields are marked *

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.

14 thoughts on “Customizing the List Search: Searching on Only One Field

  1. Hi!

    Is it possible with that system to allow a search on all fields, without forcing the choice of a specific field in the dropdown list? And if yes, is it possible to allow both systems : Search in all fields or a specific one.

    Thx for your help.

    1. This is possible using the Combo Multisearch add-on.

  2. Just a brief note…
    I was struggling with your search_fields shortcode and eventually worked out that I needed to put the fields in quotes e.g. for the situation / fields that I’m using:
    [pdb_list search_fields="resource_title, resource_notes"]

    1. Yes, any time there are spaces in the attribute value, quotes must be used. I’m sorry that wasn’t made clear.

  3. Hi Roland, great work on the plugin! it has come a long way.
    I want to know if i can do away with the search drop down list but have the search form search multiple fields. like first name and last name concurrently .

    1. You’ll need to get the Combo Multisearch add-on to do that.

      1. can i use the plugin for diff sub domains which is technically extensions of the parent site.?

  4. Hi, I’m still getting this set up, and it seems great. However I have a search problem: When I run a search it gives results. If I have to click to the second or other pages of results, it displays all database results again instead of those that are just search results. Just wondering what is going on here:

    https://growgreatfruitprogram.com/fruit-tree-database/

    Thanks.

    1. Hugh, I just tested, and it looks like it’s working fine. You could be having trouble with your cache. If you’re using any kind of page cache (not browser cache!) you should disable it for your search page.

  5. Thank you. Yes, it is working for me now also. Before I was not getting results. Thank you.

  6. My search function is not working on this page:

    http://www.fsacrylics.com/acrylicpro/

    I am interested in the upgrade to search that you offer but want to see this work first.

    Any ideas?

    1. Hi Jon,

      Can you be more specific? It tested fine for me, but I don’t know what you are expecting.

  7. Thanks – your plugin is fantastic. I am having an issue though – when I use the [pdb_list] I get the results correctly and if I click for the next set of records that also works. However if I include any search code on my page I only get the spinning wheel to the right. My database is also not being sorted by last_name like I have it configured to do or there is something wrong in the database on multiple records though in looking at the records I cannot determine what they are.
    The link is http://clubisland.org/home/?page_id=5810
    Email me private if you need the password to access this page.

    1. Hi David,

      You’re getting a “forbidden” error on the ajax call. Probably a security plugin or perhaps security setting on your site. You can solve this either by turning off AJAX (in the plugin settings under the advanced tab) or a more technical solution involves finding out what security code it getting triggered and setting up an exception so the search and sort can submit successfully. This will affect pagination of the list as well.

      I do see what looks like an alphabetical sort by last name in the list.

Leave a Reply to Jon Cancel reply

Your email address will not be published. Required fields are marked *

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.