0

I am developing filters for custom post type. Filter form is targeted to same archive page.

The problem I'm facing is if I pass particular parameter in GET then it's showing me the results if sent alone, but giving the error if passed with multiple values. Kindly see below given URLs.

Working: http://srtayade.info/testplugin/cb_college/?ownership%5B%5D=Public+Private

Not Working: http://srtayade.info/testplugin/cb_college/?ownership%5B%5D=Public&ownership%5B%5D=Public+Private

I guess it has something with % encoding. What can be the possible reason?

Basically I have created template for archive and written following code in that:

  wp_reset_query();
  $args = array(
   'post_type' => 'cb_college',
   'posts_per_page' => 10,
   'orderby'=> 'title',
   'order' => 'ASC'
  );
  if( isset($_GET['filter_submit']) || isset($_GET['location']) || isset($_GET['ownership']) || isset($_GET['exam']) || isset($_GET['facility']) || isset($_GET['course']) ||   isset($_GET['hours'])){

  $tax_query = array('relation' => 'AND');
    if (isset($_GET['location']))
    {
        $tax_query[] =  array(
                'taxonomy' => 'location',
                'field' => 'name',
                'terms' => $_GET['location']
            );
    }
     if (isset($_GET['hours']))
    {
        $tax_query[] =  array(
                'taxonomy' => 'hours',
                'field' => 'name',
                'terms' => $_GET['hours']
            );
    }
    if (isset($_GET['ownership']))
    {
        $tax_query[] =  array(
                'taxonomy' => 'ownership',
                'field' => 'name',
                'terms' => $_GET['ownership']
            );
    }
    if (isset($_GET['exam']))
    {
        $tax_query[] =  array(
                'taxonomy' => 'exam',
                'field' => 'name',
                'terms' => $_GET['exam']
            );
    }
    if (isset($_GET['facility']))
    {
        $tax_query[] =  array(
                'taxonomy' => 'facility',
                'field' => 'name',
                'terms' => $_GET['facility']
            );
    }

    if (isset($_GET['course']))
    {
        $tax_query[] =  array(
                'taxonomy' => 'course',
                'field' => 'name',
                'terms' => $_GET['course']
            );
    }
    $args = array(
       'post_type' => 'cb_college',
       'posts_per_page' => 10,
       'orderby'=> 'title',
       'order' => 'ASC',
       'tax_query' => $tax_query,
    );
  }

query_posts($args);
  • What’s the code for filtering? – Jacob Peattie Jun 4 '18 at 9:33
  • The problem doesn't have anything to do with the % encoding - they are just encoded square brackets (because you are accepting an array of options). The problem would seem to be with your script "somewhere"? If you simply reverse those URL parameters then it appears to work OK: ie. ownership%5B%5D=Public+Private&ownership%5B%5D=Public – DocRoot Jun 4 '18 at 10:29
  • @DocRoot yes you're right. It's working. But still question remains same, why it's not working irrespective of order of parameters. It's working for other parameters for multiple selections. – sushant tayade Jun 4 '18 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.