Forget it… I was updating my new EE instalation localy and inserting the updated file in the site of another client… duh… I must sleep… i must sleep…sleep is good!.
Stupid me… It worked like charm…
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 😉 )
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 😉
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 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 Do a backup of the mod.search.php file that can be found your:
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;
And edit the value ‘title’ replacing it with the first value you wrote down ‘substance’ and the “ORDER BY title” with “ORDER BY field_id_5”. FOr our example it will be like: