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:

[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.

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

  1. 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.?

  2. 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.

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

  4. 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.

  5. 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
You have to agree to the comment policy.

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

  1. 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.?

  2. 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.

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

  4. 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.

  5. 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
You have to agree to the comment policy.