We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Search Fields

Development and Programming

Mark Croxton's avatar
Mark Croxton
319 posts
16 years ago
Mark Croxton's avatar Mark Croxton

Use this plugin to search weblog entry titles, custom fields, category names, category descriptions and category custom fields.

Search parameter syntax is identical to the weblog search parameter.

In addition to entry custom fields you can search entry titles, category names, category descriptions and category custom fields. When searching categories references to category fields should be prefixed with ‘cat_’.

For example: search:cat_name=”keyword” search:cat_description=”keyword” search:cat_custom_field=”keyword”

Returns a delimited list of entry ids.

Parameters

search:[field] = (optional) Field can be title, cat_name, cat_description, [custom_field_name], cat_[custom_field_name].

weblog = (optional) Single weblog name to search. Default is * (searches all weblogs).

operator = (optional) ‘AND’ or ‘OR’. Operator for joining search field WHERE conditions. Default is ‘OR’.

delimiter = (optional) Delimiter for returned entry id string. Default is pipe |.

placeholder = (optional) Single variable placeholder to replace with search results output. Default is search_results (use as {search_results}).

dynamic_parameters = (optional) Eg: “title|custom_field”. Allow specific search parameters to set via $_POST (form fields should have same name as the fields you wish to search).

This plugin is best used as a tag pair wrapping {exp:weblog:entries}.

Example

{exp:search_fields search:title="keyword" search:custom_field="keyword" search:cat_name="keyword" operator="OR" weblog="my_weblog" parse="inward"}
    {exp:weblog:entries entry_id="{search_results}" dynamic="off"}
        <a href="http://{page_url}">{title}</a>
    {/exp:weblog:entries}
{/exp:search_fields}
       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Hi Mark,

A really nice neat little addition to all the plugins out there this one so thanks for that 😊

Any ideas how much overhead this applies to a page in getting the entries like this? I mean, having a plugin supplying the entry ids to the weblog tag like this?

Anyhow a really nice little plugin which I’m sure will come in handy when needing to search titles so thanks for the upload.

Best wishes,

Mark

       
Mark Croxton's avatar
Mark Croxton
319 posts
16 years ago
Mark Croxton's avatar Mark Croxton

There is a very small additional overhead for searching entry titles (compared to using exp:weblog:entries search:field=”) as I’m using an extra join. Searching category titles and description adds a further two joins, and searching custom category fields another join and a second query. These additional joins are only made as needed, so if you’re just searching titles the overhead is only one join. Narrowing the search using the weblog parameter will help with speed, and using the exp:weblog:entries disable parameter will allow you to reduce the total number of queries, so be sure to do that for features not required.

So this isn’t quite a replacement for the search module, it doesn’t cache results and will probably struggle if you have a very large number of entries; however, it should be fine for custom search forms on sites with a few thousand entries per weblog.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen
There is a very small additional overhead for searching entry titles (compared to using exp:weblog:entries search:field=”) as I’m using an extra join. Searching category titles and description adds a further two joins, and searching custom category fields another join and a second query. Narrowing the search using the weblog parameter will help with speed, and using the exp:weblog:entries disable parameter will allow you to reduce the total number of queries, so be sure to do that for features not required.

Yep I always add those in as standard anyway 😉 Definitely the best thing to do on any site.

So this isn’t quite a replacement for the search module, it doesn’t cache results and will probably struggle if you have a very large number of entries; however, it should be fine for custom search forms on sites with a few thousand entries per weblog.

Sounds good to me 😊

Thanks again for a great plugin addition. I’m sure many others are going to love this one.

Best wishes,

Mark

       
vibe9's avatar
vibe9
96 posts
16 years ago
vibe9's avatar vibe9

This plugin is great, however, any suggestions on how to pass values dynamically to it would be welcome!

For example, I am trying to do this:

{exp:search_fields search:title="<?php echo $_GET['q'] ?>"  search:cf_pros_firstname="<?php echo $_GET['q'] ?>" search:cf_pros_lastname="<?php echo $_GET['q'] ?>" operator="OR" weblog="<?php echo $_GET['q'] ?>" parse="inward"}
    {exp:weblog:entries entry_id="{search_results}" dynamic="off"}
        {title}|{url_title} 
    {/exp:weblog:entries}
{/exp:search_fields}

… I am trying to use it in conjunction with an ajax auto-complete plugin found here.

I hope that’s all clear 😊

       
vibe9's avatar
vibe9
96 posts
16 years ago
vibe9's avatar vibe9

Duoh! Found the solution: you have to set the template PHP parse setting to “input”.

       
Mark Croxton's avatar
Mark Croxton
319 posts
16 years ago
Mark Croxton's avatar Mark Croxton

Glad you worked it out. One very important thing I’d suggest though if you’re doing it this way is sanitize the $_GET values before passing them to the script. EE provides the Input class for this. I would also add in some checking to prevent XSS (if you plan on re-displaying the search term):

<?php
global $IN; 
$q = str_replace(array('{','}','?','\"','\\'), '', trim($q = $IN->GBL('q', 'GET')));
?>

Alternatively you can use the dynamic_parameters parameter to tell the script to grab the submitted values from the $_POST array, although you would need to duplicate the field values if you’re searching in more than one custom field for the same string.

       
vibe9's avatar
vibe9
96 posts
16 years ago
vibe9's avatar vibe9

Cool thanks for the tip. Should I include that code once in the search result template or for every instance where I’m currently using the following?

<?php echo $_GET['q'] ?>
       
Mark Croxton's avatar
Mark Croxton
319 posts
16 years ago
Mark Croxton's avatar Mark Croxton

I would suggest for every instance that you register a global variable 😊

       
Frank Harrison's avatar
Frank Harrison
154 posts
16 years ago
Frank Harrison's avatar Frank Harrison

This is just what I’m looking for. But: when I try to install it, it kills my plugin manager (page is completely blank). According to the knowledge base - http://expressionengine.com/knowledge_base/article/my_admin_plugin_manager_page_is_blank/ - that’s normally due to a PHP parse error or permissions. I’ve set permissions to 777 and still not working, so might there be an error in the code?

       
Mark Croxton's avatar
Mark Croxton
319 posts
16 years ago
Mark Croxton's avatar Mark Croxton

I’ve only tested the plugin on EE 1.6.8. It definitely won’t work for 2.0, and maybe not for earlier versions.

What is your EE version?

       
Frank Harrison's avatar
Frank Harrison
154 posts
16 years ago
Frank Harrison's avatar Frank Harrison

I’m on 1.6.8 too… was a new install but tried some other plugins and they worked ok…

       
Mark Croxton's avatar
Mark Croxton
319 posts
16 years ago
Mark Croxton's avatar Mark Croxton

Scratching my head here… try turning on PHP error messages here:

Admin > System preferences > Output and debugging preferences

After that, take a look at that plugin page and tell me what you see.

       
Frank Harrison's avatar
Frank Harrison
154 posts
16 years ago
Frank Harrison's avatar Frank Harrison

Debug Preference was already set to show messages to Super Admins. Maybe my hosts are blocking the PHP errors? Will check it out…

       
Frank Harrison's avatar
Frank Harrison
154 posts
16 years ago
Frank Harrison's avatar Frank Harrison

Have enabled PHP errors via a .htaccess. Here’s what I got:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

… Is that any use?

       
1 2 3 Last

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.