0

I have a search form that searches based on keywords (which are essentially series of serial numbers for a product) in a channel field. I would like search to go directly to entry if a valid keyword is entered in search. User would only be taken to search_results_serial template if an invalid keyword is entered that isn't found in the keywords on channel field.

Here's the search code:

{exp:search:simple_form channel="pages" category="1" no_result_page="site/search_no_results" result_page="site/search_results_serial" search_in="entries"}
    <form action="#" method="get">
        <label for="serial">SERIAL NUMBER</label>
        <div class="form-search">
            <input type="text" name="keywords" id="keywords" value="" size="18" maxlength="100" class="input-text">
            <button class="button" title="Search" type="submit"><span><span>Search</span></span></button>
        </div>
    </form>
{/exp:search:simple_form}

Any help is much appreciated. Thank you in advance.

UPDATE: Added custom plugin

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class search_redirect {

    public function __construct()
    {
        $search_id = substr(ee()->uri->query_string, 0, 32);
        $query = ee()->db->query("
            SELECT keywords 
            FROM exp_search 
            WHERE search_id = '".ee()->db->escape_str($search_id)."'");
        if($query->num_rows() == 1)
        {
            $keywords = $query->row('keywords');

            // Say your custom field which contains your serial number is field_id_10 (look in exp_channel_fields)
            $entry = ee()->db->query("
                SELECT t.entry_id, t.url_title 
                FROM exp_channel_titles t 
                LEFT JOIN exp_channel_data d 
                ON t.entry_id = d.entry_id 
                WHERE d.field_id_27 = '".ee()->db->escape_str($keywords)."' 
                LIMIT 1");
            if($entry->num_rows() == 1)
            {
                // Assuming you display your product at product/index
                ee()->functions->redirect(
                    ee()->functions->create_url('/product/'.$entry->row('url_title'))
                );
                exit();
            }
        }
    }
}

/* End of file pi.plugin_name.php */
/* Location: ./system/expressionengine/third_party/plugin_name/pi.search_redirect.php */

And added to search results template:

{embed="embeds/head"}
{embed="embeds/header"}

        <!-- START Main Content -->

        <div class="main-container col2-left-layout">
            <div class="main">
                <div class="col-main">
                    <div class="breadcrumbs">
                        <ul>
                            <li class="home"><strong>Search Results >></strong></li>
                        </ul>
                    </div>
                        <article id="content">
                        <div class="parts_table">
                          <table>
                              <tr>
                                <th>Support Documentation</th>
                                <th>Support Page Link</th>
                                <th>Serial Number Searched</th>
                              </tr>
                          {exp:search_redirect}
                          {exp:search:search_results}
                              {if count == 1}
                                <p>You searched for serial number '<strong>{exp:search:keywords}</strong>' and got {exp:search:total_results}{total_results}{/exp:search:total_results} result{if "{exp:search:total_results}" != 1}s{/if}. {if {total_results} == 1}You got an exact match!</p>{if:else}<p>Too many results? <a style="text-decoration:underline;" href="{site_url}machine-support">Try searching again with a valid serial number</a> <strong><span></p>{/if} 
                              {/if} 
                              <tr>
                                <td><b><a href="{page_url}">{title}</a></b></td>
                                <td><a href="{page_url}">View Support Page</a></a>
                                <td>{exp:search:keywords}</td>
                              </tr>

                              {if count == total_results}
                                  </table>
                              {/if}

                              {paginate}
                                  <p>Page {current_page} of {total_pages} pages {pagination_links}</p>
                              {/paginate}

                          {/exp:search:search_results}
                          </table>
                        </div>
                    </article>

                </div>
{embed="embeds/sidebar_nav_left"}
            </div>
        </div>
        <!-- END Main Content -->


{embed="embeds/footer"}
1

One note first: you don't need to add a manual form tag inside of the search:simple_form tag - it generates its own.

The best way to do this (since the Search module doesn't have great extension hooks) is to add some custom code on the search result page. This would be best done in a custom plugin.

You'd grab the keywords based on the search query in the URL, then use that to perform query for a matching serial number, and if found, do a redirect to that entry's URL.

Some quick, untested code for inside your plugin:

$search_id = substr(ee()->uri->query_string, 0, 32);
$query = ee()->db->query("
    SELECT keywords 
    FROM exp_search 
    WHERE search_id = '".ee()->db->escape_str($search_id)."'");
if($query->num_rows() == 1)
{
    $keywords = $query->row('keywords');

    // Say your custom field which contains your serial number is field_id_10 (look in exp_channel_fields)
    $entry = ee()->db->query("
        SELECT t.entry_id, t.url_title 
        FROM exp_channel_titles t 
        LEFT JOIN exp_channel_data d 
        ON t.entry_id = d.entry_id 
        WHERE d.field_id_10 = '".ee()->db->escape_str($keywords)."' 
        LIMIT 1");
    if($entry->num_rows() == 1)
    {
        // Assuming you display your product at product/index
        ee()->functions->redirect(
            ee()->functions->create_url('/product/'.$entry->row('url_title'))
        );
        exit();
    }
}

So then you'd just put this in your search results template:

{exp:my_custom_plugin}
3
  • Derek, I added a custom plugin as outlined in the link provided in your answer. Additionally, I added the custom field id, found in db, which is "27". However, I'm still not being redirected. Am I missing something? Is there some other information I can provide so you can better help me? Thank you for the help!
    – frshjb373
    Jan 29 '15 at 14:45
  • Try exiting out at different points in your plugin code to see what steps it's making it through (i.e. exit('Found the keywords!');). That should help you debug. Jan 29 '15 at 15:00
  • Think it's something right at the second if statement if($entry->num_rows() == 1)...
    – frshjb373
    Jan 29 '15 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.