Posted on by

Showing a Search Result as a Single Record

Let’s say you are using Participants Database for a database of items, each with a unique serial number. You don’t want to show any of the records until a matching serial number is entered by the user.

So instead of showing this single result as a list of one item, how about showing that result as a “single record” display? This saves the user a click, taking them immediately to the information they are looking for.

To accomplish this, we need to use a custom template for the list shortcode. It turns out, it’s pretty simple because all we need to do is grab the ID of the record and use that to show a [pdb_single] shortcode. There’s more to it, of course, but that’s the core idea.

My approach here was to look at the default list template, then remove anything I didn’t need: mostly the loops that show the records in a table, and the pagination control. I need the search control, of course. Each of these three things (search, list and pagination) can be thought of as separate units, so it’s easy to just take them out. The container that wraps them all has to stay, though.

The spot occupied in the template by the list table is where our single record will go. We do that by invoking the shortcode using a WordPress function “do_shortcode()” which just expects the string that you would normally just put in the content. The advantage there is we can use variables to fill in some of the info.

First, we get the ID of the record we’re going to show. When the list template is active, the data it uses to display the records is in an array $this->records . We grab the ID of the first one in that list. If there isn’t one (for instance when the page is first loaded or if the search was for a non-existent number) the single record will take care of that situation. We just give it the ID we find like this:

<?php $id = is_array( $this->records ) ? key($this->records) : 0; ?>
<?php echo do_shortcode('[pdb_single id=' . $id . ']'); ?>

You can see how the $id value is inserted into the string that invokes the shortcode, telling it which record to show. The rest of the template is just putting in the wrappers and the search control.

The single record needs to be wrapped in that div with class “list-container” so that the AJAX search script knows where to put the result of the search: that is what it would expect to find if there was a list of results being displayed.

In a default setup, save this template in your theme directory in a folder named “templates”: wp-content/themes/YOUR_THEME/templates/pdb-list-single.php For those who are using the Custom Template Folder add-on, put the template with the other templates you are using.

To use this template, you need to name it in the shortcode, and also add the “suppress” attribute so nothing gets displayed unless a matching search is performed:

[pdb_list search=true template="single" suppress=true]

If you need to change the text that is shown when no record is being shown, change the setting named “Record Not Found Error Message” under the Record Form tab.

One possible variation on this template would be to make it so that if there were multiple results, it would show a list, if there was only a single result, the single record would be shown using the single shortcode.

7 thoughts on “Showing a Search Result as a Single Record

  1. Hello
    Thank you so much for this modification, it works.

    But,
    when I put the search page, which has the code [pdb_list search=true template="single" suppress=true], as front page (reading as Homepage in Settings), the search button and the text field do not appear at all … nothing.

    Please help to solve this issue.
    Thank you very much.

    Farid

    1. I can’t say what the problem is, it might have something to do with what your theme does with it’s home page. This is suggested by the fact that it will work on other pages. Maybe you need to ask in your theme support forum.

  2. HI Roland same problem as before, results are showed as with any single character and list only one record included in the database. I cannot figure out what was changed.

  3. Hi Roland
    Thanks you so much! It works!
    S

  4. Hello I am using the ‘search button’ of the Participants database to lookup data from a database of participants (as a code) and I have noticed it searches every single character of the string (example: if I need to search XXXYY it finds it also if I type X or Y), which I do not want). How can I modify, if it is possible, to make the search button only the exact match (exactly only the string I want to search)?

    [pdb_list search=true template="single" suppress=true]

    [pdb_list filter="first_name=slug"]

    Thank you in advance
    S

    1. Hi Sara,

      Under the advanced tab in the plugin settings, you’ll find “strict user searching” try turning that on.

      1. Hi Roland
        thank you for your support.

        After your recent update of the plugin my database search code does not work anymore.

        I was using a code to look for a particular field, which is listed and sortable in the “Record Info Field”. After writing the content of this particula field (I am using an alphanumeric id) a list of records, the same of the “Record Info Field” was showed. Now it appears a search button with a list of records to select which I do not want. How would be possible to solve this issue? Is it possible also to come back to previous version?

        the code was:

        [pdb_list search=true template="single" suppress=true]
        [pdb_list filter="first_name=slug"]

        Thank you in advance
        Sara

Leave a Reply
You have to agree to the comment policy.

7 thoughts on “Showing a Search Result as a Single Record

  1. Hello
    Thank you so much for this modification, it works.

    But,
    when I put the search page, which has the code [pdb_list search=true template="single" suppress=true], as front page (reading as Homepage in Settings), the search button and the text field do not appear at all … nothing.

    Please help to solve this issue.
    Thank you very much.

    Farid

    1. I can’t say what the problem is, it might have something to do with what your theme does with it’s home page. This is suggested by the fact that it will work on other pages. Maybe you need to ask in your theme support forum.

  2. HI Roland same problem as before, results are showed as with any single character and list only one record included in the database. I cannot figure out what was changed.

  3. Hi Roland
    Thanks you so much! It works!
    S

  4. Hello I am using the ‘search button’ of the Participants database to lookup data from a database of participants (as a code) and I have noticed it searches every single character of the string (example: if I need to search XXXYY it finds it also if I type X or Y), which I do not want). How can I modify, if it is possible, to make the search button only the exact match (exactly only the string I want to search)?

    [pdb_list search=true template="single" suppress=true]

    [pdb_list filter="first_name=slug"]

    Thank you in advance
    S

    1. Hi Sara,

      Under the advanced tab in the plugin settings, you’ll find “strict user searching” try turning that on.

      1. Hi Roland
        thank you for your support.

        After your recent update of the plugin my database search code does not work anymore.

        I was using a code to look for a particular field, which is listed and sortable in the “Record Info Field”. After writing the content of this particula field (I am using an alphanumeric id) a list of records, the same of the “Record Info Field” was showed. Now it appears a search button with a list of records to select which I do not want. How would be possible to solve this issue? Is it possible also to come back to previous version?

        the code was:

        [pdb_list search=true template="single" suppress=true]
        [pdb_list filter="first_name=slug"]

        Thank you in advance
        Sara

Leave a Reply
You have to agree to the comment policy.