Changes are made at your own risk and are not guaranteed to work. Always make backups of the database and files prior to implementing hacks. Make a special backup of the file and/or template you are modifying so that you can roll back quickly.
Hacks are dangerous and can cause your site to stop working. They make later updates to ExpressionEngine more difficult; you should track your hacks for post-update re-implementation.
Most hacks are unnecessary, please review the Development Documentation for information on expanding ExpressionEngine via Modules, Extensions, and Plugins.
Hacks are never officially supported.
Original hack posted by latrine http://expressionengine.com/forums/viewthread/61028/
STEP BY STEP FOR THE REST OF PEOPLE that want to sort searches by a weblog custom field:
0- create the weblog, and corresponding custom fields.
For this example I have built a “substance” and a “factory” weblog custom field.
1 - Preparing the information
If you don’t know by now, EE spreads your weblog post information in several database tables that are connected by the “entry_id” value of your post.
When you publish a new post in your weblog an entry is created in (among others):
exp_weblog_titles <- that has the main information for the post (user_id, title, url_title, date, and some other stuff)
exp_weblog_data <- where the juicy stuff goes, including the values you input in your custom fields.
For programing simplicity the “field names” in this table don’t have the name you have inserted when you created the custom field. Instead this table calls the table columns by its id, for instance field_id_5; field_id_6 etc, and has a corresponding format column field_ft_5, field_ft_6 etc. [just for the record field_id_1 is the default Summary field, field_id_2 is the body, field_id_3 is the extended text wink )
So for the search to behave correctly, first you should go to:
Admin>utilities>SQLManager>Manage Database Tables
and scroll to “exp_weblog_data”
and then click the “browse” link. This will display the entire table
Look for the column name that holds the data you want to sort by, it will be the id one… for instance “field_id_5”
Write down the names of the columns you want to use… you can pick any one/number of them...(use a regular pen and paper wink
For easiness of use write them donw as
Field name -> Column name
for the purpose of this example i will use:
substance -> field_id_5
factory -> field_id_6
Now… the first part is creating the form in your template
1 - Create the search form inserting/editing the :
{exp:search:advanced_form}
<select name="orderby">
<option value="">Sort BY...</option> (the label displayed)
<option value="title">Title</option> (the title field)
<option value="date">Date</option> (the submit date)
<option value="substance">Substance</option> (the first custom field created)
<option value="factory">Factory</option> (the second custom field created)
</select>
<rest of the form... If you do a simple copy/paste of the default advanced search form into your template you should change the input selector that defines the "orderby" name that is already in place, adding the required entries on the drop donw menu...>
{/exp:search:advanced_form}
[what’s happening?? This form sends the server the information for the search query. This field “orderby” is part of the query and is parsed in the mod.search.php file]
3 - Hacking the mod.search.php
It is an innocuos hack, and shouldn’t ruin anything…
3.a ... even so, do a backup of the mod.search.php file that can be found in you your site’s directory:
http://url/sistem/modules/search/
3.b let’s get going…
Open the mod.search.php file in your text editor (preferably one that does sintax highligthing like Texmate for mac
Scroll down to line 757 or search for “set sort order”
It will be something like
/** ----------------------------------------------
/** Set sort order
/** ----------------------------------------------*/
$order_by = ( ! isset($_POST['order_by'])) ? 'date' : $_POST['order_by'];
$orderby = ( ! isset($_POST['orderby'])) ? $order_by : $_POST['orderby'];
$end = '';
switch ($orderby)
{
case 'most_comments' : $end .= " ORDER BY comment_total ";
break;
case 'recent_comment' : $end .= " ORDER BY recent_comment_date ";
break;
case 'title' : $end .= " ORDER BY title ";
break;
default : $end .= " ORDER BY entry_date ";
break;
}
$order = ( ! isset($_POST['sort_order'])) ? 'desc' : $_POST['sort_order'];
The first two lines are the lines that “capture” the value chosen on the form for the field that goes by the name “order_by”, or in option “orderby” (used in this example).
The nice part is the following lines.
switch ($orderby)
{
case 'most_comments' : $end .= " ORDER BY comment_total ";
break;
case 'recent_comment' : $end .= " ORDER BY recent_comment_date ";
break;
case 'title' : $end .= " ORDER BY title ";
break;
default : $end .= " ORDER BY entry_date ";
break;
}
As you can see this part of the program behave like a “if clause” that read the value of the $orderby variable and uses a different comand for each value. If the $order value has a value that is not specified, the order follows the last line, ordering by date.
Just copy and paste two lines (don’t forget the break;
case 'title' : $end .= " ORDER BY title ";
break;
Edit the value ‘title’ replacing it with the first value you wrote down on paper ‘substance’ and the “ORDER BY title” with “ORDER BY field_id_5”. For our example it will bhave two aditional entries (substance and factory):
switch ($orderby)
{
case 'most_comments' : $end .= " ORDER BY comment_total ";
break;
case 'recent_comment' : $end .= " ORDER BY recent_comment_date ";
break;
case 'title' : $end .= " ORDER BY title ";
break;
case 'substance' : $end .= " ORDER BY field_id_5 ";
break;
case 'factory' : $end .= " ORDER BY field_id_6 ";
break;
default : $end .= " ORDER BY entry_date ";
break;
}
It’s just:
case ‘ValueOfTheForm’ : $end.= “ ORDER BY NameofColumnOfExp_weblog_data “
ValueOfTheForm can be anything, as long as it is equal in your template and here in this file, but you should use the name of your weblog custom field so you can keep your mental sanity for later on, or for other programers that “inherit” your site.
Save the file, and upload it to your server…
WARNING: Remember that when you update your ExprenssionEngine instalation, mod.search.php is also replaced, so you will loose this hack. YOU WILL HAVE TO REDO IT!
WARNING2: THIS BEEING A HACk IS IN NO WAY SUPPORTED OR ENDORSED BY EE TEAM!
Test drive it… smile have fun!
JPCarvalhinho
Category:Hacks Category:Search
