Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Injecting custom dropdowns to the filter bar

Tagged: , ,

Viewing 10 posts - 1 through 10 (of 11 total)
  • Pekka Gaiser
    #184388

    I’m sure this must have been brought up before, but I can’t find anything. I must be using the wrong search terms.

    Search & Filter Pro works great for me, but I need to add several custom filtering options:

    – A date range dropdown (past quarter, this quarter, etc.)
    – A country dropdown (the country being a Meta field)
    – A zip code dropdown for filtering by the first digit of the code (1xxxx, 2xxxx, 3xxxxx – the zip code again being a Meta field)

    Is there an organic way, say a hook, to get these elements into the S&F search bar and then do a custom query based on them?

    I suppose some of this could be done using some very complex JavaScript fiddling, but it would be awful and hell to maintain.

    Trevor Moderator
    #184397

    For options two and three, each post would need a meta field (custom field) with the required data in, then you add that field using the Post Meta field control in the Form UI and select the correct meta key as its source (if you are using ACF, be aware that you need to choose the key name without the preceding underscore).

    The first option, a date range option, is far more problematic. Our plugin is currently designed to look at meta and taxonomy data in the database, and not to perform calculation matches, which is what this would be. It is not possible to store in your database the time period (e.g. past quarter) because that is relative to now, which is a calculation.

    as we are currently developing V3, do you wish me to add this date comparison as a feature request?

    Pekka Gaiser
    #184413

    The Meta field control indeed works for options two and three. Thank you!

    Yes, a “date range” feature would be really cool, but perhaps a bit esoteric and personally I’d already be happy with an easy way to fiddle with the query before it is run. So that I could set up a dropdown with “current quarter” / “last quarter” / etc. as selectable values and then intercept them & turn them into dates that S&F can understand before running the custom WP_query invoking the filter.

    Trevor Moderator
    #184419

    Ah. You might be able to do that. I am not sure it would work, but here you will find our public filters and actions:

    https://searchandfilter.com/documentation/action-filter-reference/

    You could setup a post meta form field as a dropdown based on the date field, but set the values as manual so that you have the options you want.

    Then you could use (I hope) our Edit Query Arguments filter and change the values passed to the query based on some PHP to create the correct date range.

    Pekka Gaiser
    #184526

    That sounds perfect! However, using that filter, I can’t seem to reach the relevant part of the query. I created a dropdown with manual values (“thisquarter”, “lastquarter”) and am seeing the selected value being passed to the search page: (my url)?_sfm_period=thisquarter

    However, when I try to get hold of _sfm_period using the filter you mentioned, it isn’t there! What I see is

    Array
    (
    [paged] => 1
    [search_filter_id] => 3136
    [search_filter_override] =>
    [posts_per_page] => 500
    [meta_query] => Array
    (
    )

    [post_type] => shops

    Which appear to be the static parts of the form but not the properly spelled out query (yet).

    Should I just add my correct date values to the meta_query array?

    Trevor Moderator
    #184585
    This reply has been marked as private.
    Pekka Gaiser
    #184766

    OK! The custom filter route didn’t work out – looking at the plugin source code I don’t think anything is ever actually done with search_filter_override. I tried looking at global $searchandfilter but that doesn’t seem really open to outside manipulation. But, I can intercept the $_GET parameters directly and do my changes there. It’s hacky, but it does the job.

    Sadly I still can’t do what I need to do, though, because the workflow I’m creating has the search bar as a permanent feature on the page and the period selector will always snap back to the default value while everything else stays in place. That’s not acceptable from a UX perspective. Say for example, I have a form with

    – A dropdown linked to an unused “period” meta field, with the values ‘last_month’, ‘last_quarter’
    – A date_from and date_to field

    Now after submitting the form, I can fiddle with $_GET, parse the period parameter and set date_from and date_to accordingly. But I have to remove $_GET["sfm_period"] because otherwise that gets used as a filter, which would of course yield 0 results! I can remove the field but then S&F will think the value is not set, and the “period” dropdown will snap back to its default value.

    What this would require is the ability to have dropdowns that aren’t actually linked to a real meta field and so what you select there doesn’t play into the query.

    Any ideas around this would be welcome.

    Trevor Moderator
    #184795

    What you ask for (towards the end) is a feature we are building in to V3, but I don’t have any timings for its release.

    Pekka Gaiser
    #184825

    Fair enough!

    Should anyone else have the same problem: I’ve managed to find a hacky but reasonable solution for now: when parsing the drop-down contents (like turning “last_quarter” into a start and an end date) I inject some JavaScript into the document that sets the dropdown to the right value.

    if (isset($_GET["_sfm_pseudofield_daterange"])) {
       
        $daterange = $_GET["_sfm_pseudofield_daterange"];
        ....  Handle the date stuff
    
        // Now remove the GET parameter so it doesn't get used as a filter and lead to 0 results
        unset($_GET["_sfm_pseudofield_daterange"]);
    
        // Use JavaScript to correctly set the dropdown again even though GET parameter was unset
        add_action( 'wp_head', function() use($daterange) {
           ?><script>jQuery(document).ready(function(){ jQuery("[name='_sfm_pseudofield_daterange[]']").val("<?= htmlentities($daterange);?>");});</script><?
    		} );
    }

    As a feature request, it would be awesome to have a new “empty” field type (Text field, dropdown, check box, etc.) that gets passed along with the rest of the form, plus a documented interface for altering query parameters in the sf_edit_query_args hook.

    Pekka Gaiser
    #184827

    To be clear for future readers, I’m doing the above before the page is getting rendered, in my case in the theme’s functions.php.

Viewing 10 posts - 1 through 10 (of 11 total)

The topic ‘Injecting custom dropdowns to the filter bar’ is closed to new replies.