Solved, the code above takes the URL and turns it into a query, and for category_name, if only 1 was used, the wordpress rewrite would swap the category_name=cat for /category/cat so I solved this by putting an empty dummy in, so the url always had two categories, one an empty one.