ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

search engine order by [now with hack tutorial]

September 18, 2007 12:03pm

Subscribe [1]
  • #1 / Sep 18, 2007 12:03pm

    latrine

    28 posts

    I put a post on the How To forum, but now I am really stuck and reporting to you guys…

    I want to be able to order the search results by a custom weblog field… ist it possible


    Just add the input field you wish in th HTML between the {exp:search} tags… for instance:

    {exp:search:advanced_form}
    
    <input type="hidden" name="sort_order" value="asc/des" />
    <input type="hidden" name="orderby" value="nameofthefield" />
    
    <rest of the form>
    
    {/exp:search:advanced_form}

    This way when the template is parsed this fields are included on the search form…

    It sounds simple, but due to a mental lock-down from my brain I spent almost two days getting this to work ...duh!

    Question for the pros:
    Can I use this mechanism to sort by a custom weblog fields? what should iI use as the value? the “name” of the field or it’s database counterpart (field_id_n)?

     

    On the advanced form pages you give some options like title, date, most coments, etc… but nothing about other fields… like custom ones…

    Thxs

  • #2 / Sep 18, 2007 12:40pm

    Robin Sowell

    13255 posts

    I cheated and just looked at the code.  I don’t think you’re going to get what you want without a hack.  Check in modules/search/mod.search.php around line 766:

    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;
            }

    So- that’s pretty much what you’re limited to.  But you could try it with a hack- allow the field you want to sort on.  I didn’t actually trace the code back to see if all the queries are including the exp_weblog_data table- if they aren’t, there would be a problem.  But- it wouldn’t take long to test it out.

    (And if we poke this anymore- I’ll probably shift it back over to ‘How to’.)

  • #3 / Sep 18, 2007 12:50pm

    latrine

    28 posts

    OK, thxs… As all the mentioned fields are in the exp_weblog_entries, I wouldn’t be surprised that the queries wouldn’t inlcude the exp_weblog_data… “ORDER BY entry_date” and not “ORDER BY exp_weblog_entries.entry_date” leave pretty low expectations but… I will try not to break much at home…  😊

  • #4 / Sep 18, 2007 12:53pm

    Robin Sowell

    13255 posts

    LOL- yea, make sure you have a nice, clean backup to replace things with.  On the plus side- it does allow searching in those fields- so maybe all the joins will work off the bat.  Can’t hurt to try.

  • #5 / Sep 18, 2007 7:53pm

    latrine

    28 posts

    Robin… If I update the mod.search.php the changes are immediately available for the entire site?

    let me explainthis doubt…


    I have been poking inside the code and I tried replacing the code you mentioned by:

    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 'nome'            :    $end .= " ORDER BY title ";
                    break;
                case 'substancia'            :    $end .= " ORDER BY field_id_6 ";
                    break;
    
                default                    :    $end .= " ORDER BY entry_date ";
                    break;
            }


    As you can see I have added the line “nome” and “substancia” and deliberately told that by selecting “nome” the sql would sort by -title- (as if I choose the default title option)

    Now on the form I have added all the possible entries, but when I choose “nome” it produces a sort by date (the default entry) and not by title,

    Is there a way to output on screen what is the $orderby value for the running query?
    I have tried to include the php print $orderby; ont mod.search.php and on my template to no avail…

    I am pointing to some kind of caching problem, that isn’t updating the search engine immediately, but I would like t o confirm this problem…

  • #6 / Sep 19, 2007 6:15am

    latrine

    28 posts

    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:

  • #7 / Sep 19, 2007 7:22am

    latrine

    28 posts

    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 AHAC IS IN NOW WAY SUPPORTED OR ENDORSED BY EE TEAM!

    Test drive it…  😊 have fun!

    JPCarvalhinho

  • #8 / Sep 19, 2007 7:31am

    latrine

    28 posts

    By the way I tried to post this on the wiki, but with no avail… i can’t create a new page!

  • #9 / Sep 19, 2007 10:16am

    Robin Sowell

    13255 posts

    Woot- glad you got it to work and thanks for sharing.  And huh- the next thing I was going to suggest was posting to the wiki.  Huh- we should figure out why that is so I can give Lisa a ‘heads up’.  I’m going to a. move this to ‘how to’; b. try to post to the wiki- see if it fails for me.

    And you’re probably beat- but if you have the energy, let me know exactly what’s happening with the wiki when you try to post.

    Thanks again for the write-up.  Someone will now doubt find this handy.

  • #10 / Sep 19, 2007 10:24am

    Robin Sowell

    13255 posts

    ETA- posted http://expressionengine.com/wiki/Order_search_by_custom_field/

    Dang- that must have taken forever for you to write up!

  • #11 / Sep 19, 2007 12:36pm

    latrine

    28 posts

    Don’t worry… I did it in my “RealLifeWorkTime” ... hehehehe

    About the wiki… I pressed ““create new page” gave it a name (fully written, with spaces), pressed “plus” and it took me back to the front page… I did it so many times I left wondering if I had given Lisa a lot to clean up 😊

  • #12 / Sep 19, 2007 12:42pm

    Robin Sowell

    13255 posts

    Hee- yea, that’s the way to do it.  I’m actually playing solitair as we speak!  Ahem.

    OK- that’s how I created the new wiki page as well- hit ‘new page’ and a rounded box popped up requesting the page title.  I’m betting you got no rounded box?  What browser are you running?

  • #13 / Sep 19, 2007 12:44pm

    Robin Sowell

    13255 posts

    Hm- and tried it with js disabled and nothing happens when I click the ‘Create a new page’ button- though edit still works.  Think that might could be tweaked.  But let me know on your browser- then I’ll pass all the info over to the crew.

  • #14 / Sep 19, 2007 12:51pm

    latrine

    28 posts

    I also could edit it for “typos” and general mistakes… but coulnd’t again create a new page… (the rounded box did show up, and I have JS enabled)

    I am using firefox 2.0.0.7 on a windows XP install ebhind a corporate agressive firewall

  • #15 / Sep 19, 2007 1:33pm

    Robin Sowell

    13255 posts

    Hm.  Odd.  But- I’m passing info along.  Thanks!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases