Skip Navigation

[Resolved] Multiple URL arguments to view filter

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 20 replies, has 2 voices.

Last updated by ELAN42 5 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#1259709

According to your documentation, it is possible to set a view filter source to be "URL parameter":
example.com/houses_page/?available=0

But what about a range of values? I only get the first value, trying something like this:
/view-id-list/?custom-id[]=107933&custom-id[]=405362

Can it be done? What is the right syntax?

#1259763

To clarify: "custom-id" is a numeric custom field.

I also tried to set an exposed filter with a multiple select. From that, I gather the URL string should be:
/view-id-list/?custom-id%5B%5D=107933&custom-id%5B%5D=405362

But still I get only the first value when the view is updated (by AJAX).

#1259953

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - have you check how range filter is configured with the following reference site:
=> hidden link

When you create a View you can filter items based on custom field values. You need to add a filter to the View query and use the BETWEEN filter.

I suggest you can go through the following tutorials:
=> https://toolset.com/documentation/user-guides/front-page-filters/
=> https://toolset.com/2014/06/learn-build-flexible-parametric-searches/

You can create test site for you from here and learn how price range filter is working:
=> http://discover-wp.com/register/?site_type=713

More info:
=> https://toolset.com/faq/how-and-why-to-create-a-test-site-in-discover-wp/

#1259981

"Between" would be fine for a range of prices ("from 100 to 200 dollars").

But what we have here is a set of predefined values. We want items matching 107933 or 405362 or other specific numbers.

#1259987

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - so do you want to match the single number at a time?

Can you please share how exactly you want to filter the results?

From where these values will come from and how you want to display such values - as select filter or checkboxes?

#1259997

What I want to provide: a URL with something like "/view-id-list/?custom-id=107933&custom-id=405362"

What I want to get: custom posts having "custom-id" (a custom field) equivalent to 107933, or to 405362

If I use /view-id-list/?custom-id=107933 or /view-id-list/?custom-id=405362
Then it works correctly, but I need both the cases, not just one.

The values do not need to be displayed (that was just a test I was making), but only provided as URL parameters.

#1260053

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to know from where you will generate such URL params?

#1260055

From a custom script, having its own logic.

#1260067

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok then what I suggest is - Try to pass the url param as:
What I want to provide: a URL with something like "/view-id-list/?custom-id1=107933&custom-id2=405362"

Then use the view's filter hook wpv_filter_query to add query filter argument on fly.

Please check the related ticket for reference, you need to adjust the code accordingly:
=> https://toolset.com/forums/topic/custom-field-query-filter/#post-404625

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#1261037

Thank you for the suggestion. I tried:

add_filter('wpv_filter_query', 'custom_search_criteria', 10, 2);

function custom_search_criteria($query_args, $view_settings)
{
    if (isset($view_settings['view_id']) && $view_settings['view_id'] == 216) {

        $index = 0;
        if (isset($query_args['meta_query'])) {
            $index = count($query_args['meta_query']) + 1;
        }

        if (isset($_GET['custom-id']) && !empty($_GET['custom-id'])) {
            $query['meta_query'] = array(
                'relation' => 'OR',
                array(
                    'key' => 'custom-id',
                    'compare' => '=',
                    'type' => 'NUMERIC'),
            );

        }

    }
    return $query_args;
}

The code is correctly recognized (I can echo some text after checking the $_GET values), but still the View only returns one item (the last one), not all the items. Can you please help me further?

#1261809

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - can you please share information that how you pass the URL params, Please share the URL you are using to catch the URL params so based on that I will try to adjust the code you shared.

#1263687

Something like "/view-id-list/?custom-id1=107933&custom-id2=405362" would be fine, but since we generate such URLS any alternative would be fine too! I need the end result (the filtering), how pretty is the URL is less relevant.

#1263819

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - what if you try to use the following code:

add_filter('wpv_filter_query', 'custom_search_criteria', 10, 2);
 function custom_search_criteria($query_args, $view_settings){
    if (isset($view_settings['view_id']) && $view_settings['view_id'] == 216) {
 
        $index = 0;
        if (isset($query_args['meta_query'])) {
            $index = count($query_args['meta_query']) + 1;
        }
 
        if (isset($_GET['custom-id1']) && !empty($_GET['custom-id1'])) {
            $query['meta_query'][$index] = array(
                array(
                    'key' => 'wpcf-custom-id',
                    'compare' => 'IN',
                    'value' => array($_GET['custom-id1'],$_GET['custom-id2']) ),
            );
 
        }
 
    }
    return $query_args;
}

Please replace your original custom field slug with 'key' if needed.

#1263847

Sorry, I copy-pasted the URL you suggested in your previous reply and didn't notice the limit of the numbers (custom-id1 and custom-id2). What you are suggesting would be fine if we had just two possible custom-id, but we can have many, many more. What I need is a way to get all the values in the custom-id[] array, with any length it might have.

#1263861

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - so then you should try to generate the IDs with comma separated:

Something like "/view-id-list/?custom-id=107933,405362,9999,5888,7777 

So, if you are able to to generate such comma-separated IDs with URL param custom-id , then you should try to use the same code I shared before and you just need to change the URL param values.

For example:

add_filter('wpv_filter_query', 'custom_search_criteria', 10, 2);
 function custom_search_criteria($query_args, $view_settings){
    if (isset($view_settings['view_id']) && $view_settings['view_id'] == 216) {
  
        $index = 0;
        if (isset($query_args['meta_query'])) {
            $index = count($query_args['meta_query']) + 1;
        }
  
        if (isset($_GET['custom-id']) && !empty($_GET['custom-id'])) {
            $query['meta_query'][$index] = array(
                array(
                    'key' => 'wpcf-custom-id',
                    'compare' => 'IN',
                    'value' => array($_GET['custom-id']) ),
            );
  
        }
  
    }
    return $query_args;