Skip to content
Advertisement

I am trying to use AJAX to change the orderby and order arguments in a WP_Query, but I can’t determine why my code won’t work [closed]

So I’ve been working on this for hours now. About 6 of them with no breaks and I cannot understand why this isn’t working.

So I have this form:

JavaScript

Whenever you change one of the radio buttons, AJAX is triggered using this JS code:

JavaScript

And here’s the AJAX function:

JavaScript

I have used this code before on another website, so I know that it can work, but it’s not working in this instance.

The country_filter and region_filter radio buttons are working. When you change them the AJAX call runs, and the wp_query is actually filtered.

The problem, however, is that the sort_filter radio button won’t work for some reason.

So, to test I’ve been putting print_r("some random bit of text") and checking the network tab in my browsers inspector to see if that text appears in the Request. Under if(isset($_GET['country_filter'])) and if(isset($_GET['region_filter'])), the text displays. But, if I put it under if(isset($_GET['sort_filter'])) it doesn’t display.

So, clearly, the AJAX code is getting to the if(isset($_GET['sort_filter'])) line and saying “Nope, it’s not set”. But it is.

And if I use console.log() in the JS code to log the data that is being sent to the AJAX call, I can clearly see that the value of the selected sort_filter radio button is being sent:

JavaScript

The request URL in the Network tab also has that information in it, for example:

JavaScript

Despite all that, I can’t get the AJAX code to $GET the sort_filter radio button value and use it in the WP_Query.

Any help is totally appreciated. I’m going to get some shut-eye for a bit as I’ve completely burnt myself out on this but I look forward to seeing what help anyone can offer because this really is driving me up the wall and the deadline is fast approaching.

Enormous thanks in advance!

Advertisement

Answer

In your ajax callback function you have

JavaScript

and it should be

JavaScript

ie you are missing the _ out of both calls to $_GET

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement