Skip Navigation

[Resolved] Custom post filter dropdown works first time, url getting longer each submit

This thread is resolved. Here is a description of the problem and solution.

Problem:
Custom post filter dropdown works first time, url getting longer each submit
Solution:

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/custom-post-filter-dropdown-works-first-time-url-getting-longer-each-submit/#post-1775385

Relevant Documentation:

This support ticket is created 4 years, 12 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 9 replies, has 2 voices.

Last updated by ernestL 4 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1772419

Hi Support
I have a dropdown in a search form for selecting a custom post type. The search works regardless of what I select the first time. But after that, the selection does not work. It seems as though the URL is get longer each time, with multiple instances of the parameters being added to the end.

I am using this code in my functions file that I found on your forums:
{code}
add_filter( 'wpv_filter_query', 'post_type_filter_func', 10, 3 );

function post_type_filter_func( $query_args, $settings, $view_id ) {
if ( $view_id == 36220 && isset($_GET['wpv-post-type'][0]) && !empty($_GET['wpv-post-type'][0]) ) {
$query_args['post_type'] = $_GET['wpv-post-type'][0];
}
return $query_args;
}
{/code}

This is the form info I have in my view.
{code}
[wpv-control url_param="wpv-post-type" type="select" values=",journal-article,policy-issue,research-report" display_values="All,Journal Articles,Policy Analysis,Research Report" class="search-research-and-resources-dropdown"]
{/code}

If I select any of these items the first time it works fine. But any change to the dropdown and clicking the submit button again, causes the parameters to continue to be added to the url. I believe the function code is picking up the first occurrence of wpv-post-type since I see that being added each time.
See URL examples below.

FIRST SEARCH SUBMISSION (Select Journal Articles):
hidden link

SECOND SEARCH SUBMISSION (Select Policy Analysis - Results stay on Journal Articles - 1st item)
hidden link

THIRD SEARCH SUBMISSION (Select Research Reports - Results stay on Journal Articles - 1st item)
hidden link

How do I get this url to reset after I select a new item in the dropdown and submit the search again?

Just a note: This was working fine. But we updated the plugin a couple of days ago. Could that have caused this to break? Not sure if that caused this?

Thanks!

#1773165

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share admin access details so I can see whats going wrong with your setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1775385

Minesh
Supporter

Languages: English (English )

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

I've moved the code you added to child theme's functions.php file to "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_filter_query', 'post_type_filter_func', 10, 3 );

function post_type_filter_func( $query_args, $settings, $view_id ) {
    if ( $view_id == 2594 && isset($_GET['wpv-post-type'][0]) && !empty($_GET['wpv-post-type'][0]) ) {
        $query_args['post_type'] = $_GET['wpv-post-type'][0];
    }
    return $query_args;
}

Then, I've added the following JS code to your view's "Search and Pagination" section's JS Editor:
=> hidden link

jQuery(document).ready(function($){
  jQuery("form input[type='hidden'][name='wpv-post-type[]']").remove();
});

Now, the thing is that if I switch to default twentytwenty theme, the URL param works as expected and I can see only one URL param when I try to filter by post type.

But, when I switch to your child theme and try to filter with posttype:
=> hidden link
I see the following JS error:

Uncaught TypeError: Cannot read property 'msie' of undefined
#1776419

I tried what you described. Is this an issue with the theme or the wordpress update? This was all working fine for months. Last week we updated to wordpress 5.5.1. Any thoughts on a workaround?

#1776661

Minesh
Supporter

Languages: English (English )

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

As you can see the JS error coming from theme file. Can you get in touch with the theme author and check with them for possible solution.

#1777857

Yes, I will contact them to check if there is a fix.
In the meantime, I researched this error and found a snippet of code that seems to allow the dropdown to work. I added it above the Javascript you added. Do you feel this is a valid solution?

jQuery.browser = {};
(function () {
    jQuery.browser.msie = false;
    jQuery.browser.version = 0;
    if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
        jQuery.browser.msie = true;
        jQuery.browser.version = RegExp.$1;
    }
})();

jQuery(document).ready(function($){
  jQuery("form input[type='hidden'][name='wpv-post-type[]']").remove();
});
#1778205

Minesh
Supporter

Languages: English (English )

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

Yes - that is Ok as the script you added will be added to the page where you added your view that means it will not be affected on the other pages.

#1780497

This is resolved. We contacted the theme developer and needed to update. With the update, the additional code is no longer needed. But still needed the code you provided.
Thank you for your help on this!
Mick

#1780499

My issue is resolved now. Thank you!