• I have a page / archive listing custom posts which have a number of custom taxonomy fields – one of the custom taxonomies is ‘Region’. In the archive-post-type.php I have drop down lists for each custom taxonomies at the top of the page, to allow the user to pick one value to only filter posts – to show posts where the taxonomy value equals their selected values

    I have decided (rightly or wrongly?) to pass in the (single) region I want to filter on as a URL paramter ?region=123 where 123 is the taxonomy term ID.

    I extract the parameter from the URL at the very top off the php code thus:

    $region = (isset ( $_GET ['region'] )?$_GET ['region']:false);

    and I set the taxonomy part of the args array for my WP_Query->query($args):

    if ($region) {
      $args ['tax_query'] = array (
        array (
          'taxonomy' => 'regions',
          'terms' => $region,
        )
      );
    }

    The drop down field is produced by:

    $region_dropdown_args=array('id'=>'filter_region','hide_empty'=>0,'taxonomy'=>'regions','hierarchical'=>1,'show_option_all'=>'(All Regions)',);
    if ($region) $region_dropdown_args['selected']=$region;
    wp_dropdown_categories($region_dropdown_args);?>

    So this all works – If I construct a URL with ?region=888 I only get the posts which have region 888.

    What is the best way to create the new URL and get the HTTP GET back to wordpress? Options as I see it are:
    1) Add javascript code like:

    to construct the URL parameters and then onclick=”window.location.href='<?php echo home_url()?>/my-archive-page/’+get_filter_url_params();”

    and I wp_enqueue_script a .js file that contains

    function get_filter_url_params() {
      var filter_ids_array=['region'];// other taxonomies go here,'other1','other2'];
      var filter_ids_length=filter_ids_array.length;
      var filter_id='';
      var url_parameters='';
      var html_element;
      for (var i=0;i<filter_ids_length;i++){
        filter_id=filter_ids_array[i];
        html_element=document.getElementById('filter_'+filter_id);
        if (html_element) {
          if (html_element.selectedIndex!=0) {
            if (url_parameters=="") {
              url_parameters='?';
            } else {
              url_parameters+='&';
            }
            url_parameters+=filter_id+'='+html_element.options[html_element.selectedIndex].value;
          }
        }
      }
      return url_parameters;
    }

    I could equally add this javascript to the drop down onchange event, but I want them to be able to select more than one taxonomy’s filter value before applying the filters.

    There are many plug ins that offer this type of functionality, but it is pretty easy to do without any plugins, and I want to do some more complicated transformation of the chosen filter values before applying the filter rules in the tax_query passed to the WP_Query.

    My questions for discussion:
    — ——— — ———–

    Would it be better to use the ‘filter’ button to post a form, and use some php to extract the POST values and construct the URL? I avoided this because this would take one POST followed by a redirect, instead of just one GET using the method above.

    Is there a more “wordpressy” way to do this? Is there a wordpress native way to handle adding arguments that I don’t know about?

    How would I get the wordpress server to ‘remember’ the choices made by the user when they later return to this page?

  • The topic ‘Best way to re-open a page with added URL parameters from taxo form fields?’ is closed to new replies.