Home Forums Feature Requests Search Form for ACF for Custom Post Types


Search Form for ACF for Custom Post Types

  • So for example, my site has many custom post types.

    • Events
    • Venues
    • Festivals
    • Line Ups

    Let’s take events. This has ACF fields of:

    • Start Date (Date)
    • End Date (Date)
    • Venue (Relationship)
    • Location (Relationship)
    • Checklist (Checkbox)
    • Tickets (Repeater)

    I’ve yet to find an easy (any!) way to have a form on the events archive page that can search this data. By this I don’t mean a single search box that searches all these fields. What I mean is:

    • Search box for title of event
    • Two search boxes to find events between two dates
    • Text field for a location
    • Checkboxes for my checklist
    • Etc

    Has this been accomplished already and I just haven’t seen it?

  • How to do this yourself is a long and complicated discussion. If you want to build it then you should start by searching for how to create a parametric or faceted search in WordPress.

    There are tools that will let you do this without the work. One of the is, there are others.

  • Shame there isn’t any direct support for it here – it would help massively if it was, even via a paid plugin.

  • There have been several topics started here about creating this type of search feature usually referred to either a parametric or faceted search. Most of the plugins available are all premium but there might be some that can do what you want that are free. In my experience the free ones are not very good or have other problems. Even the ones you have to pay for are not perfect and will require that you compromise on some things.

    I’m in the process of building a plugin and I’m building it with ACF, doesn’t do you any good now, and it’s a long way from completed. I do know what’s involved, but explaining it on a forum would not be easy.

    There is information available on how to do this, search for something like… how to build a parametric or faceted search in wordpress

  • @rockgeek

    Shame there isn’t any direct support for it here

    There isn’t because this is not related to ACF directly. It’s related more to the WordPress database structure (on which ACF relies) than to ACF itself.

    Implementing such a functionality doesn’t depend on how the post and fields have been created. So it makes no difference if you defined field using ACF, other plugin or even manually using WordPress native custom fields support.

    Firs of all you should read about WP_Query and how to write meta queries. If you want to filter existing query (add/remove/modify) then pre_get_posts filer will be useful.

    If you want I can send you a simple example of a search form built this way.

  • @rockgeek may I advise you to look into Ultimate WP Query Search Filter ?

    That works real nice out of the box, except it has some issues with checkboxes, because UWPQSF expects a single value and not a serialized array, but I found a solution for it, which I posted on my site.

    I’m not sure if this would work for you but it’s definitely worth looking into.

  • When dealing with any type of ACF field that does not store simple text values in the database, you will always have some problems, unless the plugin you’re using is built specifically to work with ACF.

    The fields that do not cause issues are the ones listed under “Basic” and a few others like single select fields, radio button fields, and true false fields.. maybe a couple of others.

    To use other types of fields in a standard WP_Query bases search application what you need to do is build filters, using the acf/save_value hook, and store the values into the meta table in a form that WP can easily search and then use these fields for the queries.

    As a quick example, let’s say that you have a checkbox field. This type of field stores a serialized array. This is difficult to search using a standard WP query, the the results can be inconsistent. But if you take the value and store in in the way that WP does, with each of the values having a separate row in the database then it becomes much easier.

    // add a filter to convert the value of a checkbox field
    add_filter('acf/update_value/name=my_checkbox_field', 'convert_checkbox_to_queryable_form', 10, 3);
    function convert_checkbox_to_queryable_form($value, $post_id, $field) {
      $queryable_name = 'queryable_my_checkbox_field';
      // first remove all of the values from the new field
      delete_post_meta($post_id, $queryable_name);
      if (empty($value)) {
        // there is not value so there's no reason to continue
        return $value;
      // now we can loop over the checkbox array
      // and store them so the values are easier
      // to use in WP_Query()
      foreach ($value as $single) {
        // add the post meta
        // and allow it to have multiple values
        add_post_meta($post_id, $queryable_name, $single, false);
      // make sure you return the original value
      return $value;
    } // end function

    With a function like this in place you can use the new field name and it makes it much simpler for meta_query.

  • @hube2 I like the idea, but will there be a different row for just each checkbox value (defined in ACF field groups) or will there be an extra row for each checkbox field in each post ?

  • There will be a different row for each value in each post. $post_id sets this meta key for the current post.

  • So if I would have let’s say 30 fields per post (of which at least 15 are obligated) and I have 1000 posts, that means I would have another (15 fields x 1000 posts =) 15000 extra table rows ???

  • Yes, but why should there be a concern about the number of table rows. Both the meta_key and the meta_value are indexed columns and the number of rows in the database will have negligible effect on the query time. And it also seems that you’re example is a bit extreme, 30 checkbox fields associated with a single post with 15 values each.

    If you were not using ACF and you constructed you’re meta fields yourself and stored them yourself, this is the same way that the values should be stored if you want to be able to easily search those fields, this is how WP works. Is the situation ideal? Probably not, but if you choose to use WP then you need to function withing the boundaries that it sets.

  • And I should also add that, by reducing the complexity of the query and removing “LIKE” statements (which are extremely slow) from them you will actually be increasing the performance of them because each query will actually take less time even if there are more rows in the database.

  • I’m not claiming my solution is the best. It is what I came up with to ‘overcome’ the serialized problem and I’m happy it works.

    I have to look deeper into your solution. It does seem interesting. And I have to admit, I miscalculated. I counted all fields in a group instead of just the checkboxes, which is only 2 or 3…. so that would mean a significant difference in amount of rows.

  • I’m not saying it isn’t. I’m just offering alternatives to things that can be difficult hurdles when dealing with searching and querying some of the more complicated ACF field types. Rather than expect a search plugin of any kind to deal with these special fields I take the approach of making the fields easier for these plugins to use when they need to use them. I would not do something like this for every complex ACF field, only those that I need to use in a query.

    I also posted a similar solution for making an acf repeater sub field easier to query ( Overall, I like my queries to be less complex and easier to figure out. The reason being self defense. I’m going to have to look at it again eventually, usually after a long enough time that I really don’t remember what it was I did and try to figure out what I was thinking when I built it. I dislike complex code that takes longer to figure out how to change it than it does to make the change.

Viewing 14 posts - 1 through 14 (of 14 total)

The topic ‘Search Form for ACF for Custom Post Types’ is closed to new replies.