Changes to Wordpress plug-in Custom Fields Search

This post is about changes I have made to the Wordpress plug-in Custom Fields Search by Don Benjamin. Big thanks to Don for writing should a well structured framework for the plug-in – I don’t think I would have even started these changes if his code wasn’t so well laid out in the first place.

Let me say up front that these changes were done to scratch my particular itch. In particular I am only using the plug-in for a single search (no presets) and only on a static page (using the tag code). So I haven’t tested outside of those parameters. I have passed these changes onto Don and if he approves of them I assume he will issue a new official release. I am offering them here now for people who need the functionality now and are willing to test it and understand it may break and may not work at all in your particular situation.

I will fix any bugs in the basic functionality outlined below (please comment with any bug reports) but I am not interested in adding anything more new, so no requests please :) The one exception to that is linked fields which I am going to spend a couple of days looking at and will post a new blog entry on here if I get it working. I will also post an entry on some code I did to group the search results into collapsible groups, something that is outside the scope of this plug-in.

The main changes can be viewed by clicking on the Test Search tab above – change the Area to Dublin to see a new (checkboxed!) question appear based on a rule. The admin for this question looks like this:

The changes are incorporated in a full zip file at https://docs.google.com/leaf?id=0B8zLeJTa7rpVMmEzNTZhYzMtZTBmMS00NWQ1LWJkNTYtOTM5NDhiY2QxZDM2&hl=en

And so on to the changes…

Added Checkbox input type

The plug-in only has single value input types available (dropdown box, radiobutton and text). I have added in a Checkbox type. The setup for the options is the same as dropdown/radiobutton. Note that the system OR’s the options – if you create a question with a choice of Blue, Green and Red and the user ticks Blue and Red the search will find all posts that have either Blue or Green for that question. I have only tested this for custom fields but it should work with other data types (e.g. Category). The checkbox widget works correctly with the conditional logic Rules – rules that reference a checkbox field are checked again each selected value.

Added ability for posts to be marked as relevant to all possible queries for a particular field.

The current plug-in works such that if a value is supplied in a particular search field, a post must “compare” to that value, with various “comparison” operators available, such as Equals, Contains, Greater Than etc. Unfortunately it doesn’t have the concept of Equals-Or-Any, i.e. the ability to say a particular post can be marked as ALL for a particular question and that post will appear in a search result no matter what the search term is for that field. I have added this ability by adding a “Allow ALL Match” checkbox in the question setup. If this is ticked, the resulting SQL query is amended to add an “OR” condition for posts with the value “ALL” for that particular field. This will probably only be useful for custom fields, though it will be applied to other data types if ticked. The word “ALL” is put through the translator.

An example of how this might be useful is where you wish to include introductory text at the start of a particular section in the search results. So if it was a real estate type system you might want a brief blurb about the particular location the user has chosen. If you create this post with the Location question set as the particular location name and all other fields set as “ALL” then it would appear whenever the user chooses that location, regardless of the answers given by the user to the other questions.

Added the ability to sort the search results

The plug-in uses the standard wordpress sort order of “Post Date” for its search results. I have added the ability to create a special type of question that isn’t shown but instead decides the search order of the search results. This is done by choosing one of the special Widgets “Order By Field Ascending” or “Order By Field Descending”. In either case it will order by the Data Type/Data Field chosen for that question. If Numeric is ticked the order will be numeric (i.e. +0 will be added to the SQL order). So, for example, if you wanted to order by Category, simply create a new question with Widget “Order By Field Ascending” and Data Type of Category. The other values for the question are not used. Note that you can have multiple Order By questions, they are read in order from first to last. For example if you had a Category order question and then a Post Date order question, the search would be ordered by Category and within each Category by Post Date.

Added a conditional rule

I have added a rudimentary conditional login to the questions. The system allows for a single rule for each question that will show or hide the question depending on the condition of other questions. The full set of rules are checked each time the user changes an answer and so questions will slide in or out of view as they answer.

To use the rule, check the “Apply Rule” Checkbox and fill in the rule details. For example if you wanted this question to only show if the answer to the question labelled “Marital Status” was “single” then you would chose “Show”, “Marital Status”, “Equals”, “Single” for the rule. For coding simplicity the rule value to be checked is entered as a freeform value rather than applying any restriction the applicable question may have – so even if the Marital Status question has a dropdown forcing the user to choose either Single or Married, the rule doesn’t offer this dropdown, it allows you to type anything in as the value to be checked. All rules are checked when the search form is first shown so any fields that do not meet the initial requirements will not be displayed at first.

Added the ability to resort the question order

My particular project will have a lot of questions that may be added to/deleted as the requirements are refined. This is awkward to do currently as the Add Field function just adds new fields to the end of the questions.If you wanted to insert a question you effectively had to redo all the questions from there on. I have therefore added the ability to move the questions around – literally ☺ You can drag the questions up and then and re-position them. The resulting search form will show the questions in the new order. Note that to move a question into the top or bottom slot you may need to “hold” it nearer to the top/bottom (respectively) of the field.

Added the ability to group questions together on the search form

It can be a useful visual aid to show questions grouped together so I have added “fieldsets” to the search form. To use these, simply give the questions a Group Name. All contiguous questions having the same name will be grouped together in a fieldset with a Legend of the group name. The main fieldset is styled using the “field” class and the legend is styled using the “ledge” class. To omit this feature don’t use any group names.

Allowed for checkboxed custom fields on post

I am using Magic Fields custom fields on my project, mainly to restrict the set of values the poster can use for custom fields on a post. Magic Fields allows for multiple entries on a field (i.e. a checkbox field). Because of the way Magic Fields stores these checkbox values, you can only use the “Like” operator if you wish to search against a Magic Fields checkboxed custom field. Using this approach allows a post to appear in multiple different search results – for example a real estate system might allow a post to be marked as having two locations where the house straddles those areas. Note I have also modified the “Like” operator to correctly implement the “Allow ALL” functionality against a Magic Fields checkbox custom field.

Tidied up admin form
As I have added a few more options for each question, I have tried to tidy up the admin section. This area isn’t exactly my forte so you may not find it an improvement ☺

Various Fixes

Among other things, I am not sure the auto added ANY functionality worked properly. I have fixed it and added an auto ANY to the Radiobutton widget as well.

Share this post...
  • Facebook
  • Twitter
  • email
  • Add to favorites
  • LinkedIn
  • Google Bookmarks
  • FriendFeed
  • StumbleUpon
  • del.icio.us

4 comments to Changes to Wordpress plug-in Custom Fields Search

  • This looks like it would give me exactly what I need in terms of search functionality, but not sure it’s works with the most recent version of wordpress. I’m not able to get to all of the settings shown on the screen shot. Is there a quick fix for this?

  • admin

    Hi Lynn,

    Believe it not, yours is the first real comment out of the 1000’s of spam comments this post has attracted over the years :D

    I did this work as part of a research project two years ago and unfortunately I haven’t worked with the plugin since then. If it doesn’t work in the current version then I can offer no solution.

    I hope the code might offer you some guidance if you decide to try and implement it in the current version.

    Cheers,
    Colm

  • For Lynn

    yes there is a solution for wp3.5 it is here
    http://wordpress.org/support/topic/not-working-on-wp35-error-unrecognized-expression-config-form-i-0?replies=7

    For Colm

    The plugin is alive still!
    If you can take your mind back: is there a solution to make the checkbox selection into logical AND not OR?

    Thanks

  • admin

    Hey,

    Apologies for delay in replying, this site gets dumped on with spam and i only clear it out every once in a while.

    A quick scan of the code suggests the following lines in extra_search_fields.php are key…

    // Changes to OR for second and subsequent values. Used e.g. for checkbox
    $restrict = $conjunction.$restrict;
    if($conjunction = "AND (") $conjunction = "OR ";

    I even left a comment in to remind me what it was doing! To change it to AND, change the key line to…

    if($conjunction = "AND (") $conjunction = "AND ";

    Note that you can’t simply delete the line, you need to get rid of the opening bracket from the first “AND(“. I can’t quite recall why it is grouping the restrictions into one bracket there but I’d say leave it like that. I think it may only be relevant to making the ORs give one true/false result but it wouldn’t hurt to bracket with ANDs I don’t think.

    I haven’t tested this and don’t have the time to, but I figured you might like some indication as to how to proceed.

    Colm

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>