• Resolved julian87

    (@julian87)


    I have a question is it possible to preset the filter via query param like /?tags=shirts,red

    I made a hacky workaround that works but I would like to know if there is a elegant solution provided by the plugin?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author WP Smart Widgets

    (@nomade123456)

    Hi @julian87,

    Yes, it’s possible to preset the filter via query parameters, although the plugin doesn’t currently have a built-in option for this. At the moment this can be done with javascript to simulate clicks on the filter form.

    Here’s a small jQuery snippet you can use as reference based on a ?tags= query parameter. It includes a short delay and a few retries to make sure the filter widgets are ready:

    <script>
    jQuery(function ($) {
        var urlParams = new URLSearchParams(window.location.search);
        var tagsParam = urlParams.get('tags'); // the URL parameter
        var maxAttempts = 10;
        var attempt = 0;
    
        function tryClickTagCheckboxes() {
            if (!tagsParam) return;
    
            var values = tagsParam.split(',').map(function (v) {
                return v.trim();
            });
    
            var success = false;
    
            values.forEach(function (val) {
                var $checkbox = $('.bpfwe-filter-item[name="tags"][value="' + val + '"]');
                if ($checkbox.length && !$checkbox.prop('checked')) {
                    $checkbox.click(); // simulate user click
                    success = true;
                }
            });
    
            if (!success && attempt < maxAttempts) {
                attempt++;
                setTimeout(tryClickTagCheckboxes, 200);
            }
        }
    
        setTimeout(tryClickTagCheckboxes, 300);
    });
    </script>

    With this, you can pre-filter your list by adding something like /?tags=shirts,red to the URL. The same approach can be adapted to other filters that require a click.

    Let me know if this work for you or if you’d like a version with no hardcoded parameters.

    Regards,
    Dara

    Thread Starter julian87

    (@julian87)

    Thanks! I adapted the code a little bit and it works fine. But would be nice as a new feature for a future release to have a native filtering by query param.

    Thanks for your great work and fast response!

    Plugin Author WP Smart Widgets

    (@nomade123456)

    Hi @julian87,

    I’m glad the snippet worked for you and that you were able to adapt it to your needs. It is a feature I will consider adding in future releases.

    Thanks again for your feedback and support.

    Regards,
    Dara

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Default filter value by query param’ is closed to new replies.