Search code examples
phphtmlsearchexpressionengine

Expression Engine Advanced Search - Problems searching for keyword


I'm having a little trouble with expression engine searching! I've got a drop down form set-up that works great, however I need to add an OPTIONAL keyword field into this search form. Any ideas how i'll do this with my current code:

Main form code:

<form method="post" action="/properties/search-results/">

            <p>Keywords:</p>
            <input id="keywords" type="text" name="keywords"/>

            <p>Town:</p>
            <select id="town" name="cat[]" multiple="multiple">
                <option value="" selected="selected">Any</option>
                {exp:channel:categories channel="property" style="linear" category_group="1"}
                <option value="{category_id}">{category_name}</option>
                {/exp:channel:categories}
            </select>

            <p>Property Type:</p>
            <select id="propertyType" name="cat[]">
                <option value="" selected="selected">Any</option>
                {exp:channel:categories channel="property" style="linear" category_group="2"}
                <option value="{category_id}">{category_name}</option>
                {/exp:channel:categories}
            </select>

            <input style="margin-top:20px; width: 100px;" type="submit" name="submit" value="Search"/>
        </form>

Search-results template:

<?php
// Grab the categories selected from the $_POST
// join them with an ampersand - we are searching for AND matches
$cats = "";
foreach($_POST['cat'] as $cat){
// check we are working with a number
if(is_numeric($cat)){
$cats .= $cat."&";
}
}
// strip the last & off the category string
$cats = substr($cats,0,-1);
?>

{exp:channel:entries channel="property" dynamic="on" category="<?php echo($cats);?>" orderby="date" sort="asc"}

I need the keyword field to search in the {title} of my entries!

Thanks for any help!


Solution

  • Try this: first install the Search Fields plugin. (You need this because the native EE "search:field_name" parameter only works on custom fields, not entry titles.)

    Then use this revised code:

    <?php
    // Grab the categories selected from the $_POST
    // join them with an ampersand - we are searching for AND matches
    $cats = array();
    foreach($_POST['cat'] as $cat)
    {
        // check we are working with a number
        if(is_numeric($cat))
        {
            $cats[] = $cat;
        }
    }
    $cats = implode('&', $cats);
    
    if(!empty($_POST['keywords']))
    {
        $keywords = trim($_POST['keywords']);
    }
    ?>
    
    <?php if($keywords) : ?>
    {exp:search_fields search:title="<?php echo($keywords);?>" channel="property" parse="inward"}
    <?php endif; ?>
        {exp:channel:entries channel="property" <?php if($keywords) : ?>entry_id="{search_results}"<?php endif; ?> category="<?php echo($cats);?>" orderby="date" sort="asc"}
            {!-- do stuff --}
        {/exp:channel:entries}
    <?php if($keywords) : ?>
    {/exp:search_fields}
    <?php endif; ?>