
Query Vars
functions.php
<?php function custom_query_vars_filter($vars) { $vars[] .= 'bedrooms'; return $vars; } add_filter( 'query_vars', 'custom_query_vars_filter' );
Loop
<?php $bedrooms = get_query_var('bedrooms'); $args = array( 'posts_per_page' => -1, 'post_type' => 'property', 'orderby' => 'title', 'order' => 'DESC', ); if ($bedrooms >= "1" ) { $args['meta_query'] = array( // 'relation' => 'AND', array( 'key' => '_bedrooms', 'value' => $bedrooms, 'compare' => '=', ) ); }
Or
<?php // functions.php function custom_query_vars_filter($vars) { $vars[] .= 'ecs01'; $vars[] .= 'ecs02'; $vars[] .= 'ecs03'; $vars[] .= 'ecs04'; $vars[] .= 'ecs05'; $vars[] .= 'ecs06'; return $vars; } add_filter( 'query_vars', 'custom_query_vars_filter' ); // loop $ecs01 = sanitize_text_field(get_query_var('ecs01')); $ecs02 = sanitize_text_field(get_query_var('ecs02')); $ecs03 = sanitize_text_field(get_query_var('ecs03')); $ecs04 = sanitize_text_field(get_query_var('ecs04')); $ecs05 = sanitize_text_field(get_query_var('ecs05')); $ecs06 = sanitize_text_field(get_query_var('ecs06')); if (! empty($ecs01) || ! empty($ecs02) || ! empty($ecs03) || ! empty($ecs04) || ! empty($ecs05) || ! empty($ecs06) ) { $ecs = array($ecs01, $ecs02, $ecs03, $ecs04, $ecs05, $ecs06); }; $args = array( 'post_type' => 'electric-locations', 'posts_per_page' => -1, 'post_status' => 'publish', ); if (! empty($ecs) ) { $args['tax_query'] = array( array ( 'taxonomy' => 'electric-locations-category', 'field' => 'slug', 'terms' => $ecs, ) ); } ?> <?php $the_query = new WP_Query( $args ); ?> // ... loop
To output the URL with the var string use a confirmation page in gravity forms and pass the variable, something likie this
ecs01={Categories (Company):1.1}&ecs02={Categories (Development Opportunities):1.2}&ecs03={Categories (Gigaplant):1.3}&ecs04={Categories (Innovation Hub):1.4}&ecs05={Categories (Networks):1.5}&ecs06={Categories (University and Colleges):1.6}
Or use your own form element like this:
<script language="JavaScript"> function toggle(source) { var checkboxes = document.querySelectorAll('#cat-form input[type="checkbox"]'); for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i] != source) checkboxes[i].checked = source.checked; } } </script> <form method="get" id="cat-form"> <fieldset> <input type="checkbox" name="ecsall" id="ecsall" onClick="toggle(this)" <?php if ( ! empty($ecs01) && ! empty($ecs02) && ! empty($ecs03) && ! empty($ecs04) && ! empty($ecs05) && ! empty($ecs06) ) { echo "checked"; } ?> /> <label for="ecsall">Toggle All</label> <input type="checkbox" name="ecs01" id="ecs01" value='company' <?php if ( $ecs01 == "company" ) { echo "checked"; } ?> > <label for="ecs01">Company</label> <input type="checkbox" name="ecs02" id="ecs02" value='development-opportunities' <?php if ( $ecs02 == "development-opportunities" ) { echo "checked"; } ?> > <label for="ecs02">Development Opportunities</label> <input type="checkbox" name="ecs03" id="ecs03" value='gigaplant' <?php if ( $ecs03 == "gigaplant" ) { echo "checked"; } ?> > <label for="ecs03">Gigaplant</label> <input type="checkbox" name="ecs04" id="ecs04" value='innovation-hub' <?php if ( $ecs04 == "innovation-hub" ) { echo "checked"; } ?> > <label for="ecs04">Innovation Hub</label> <input type="checkbox" name="ecs05" id="ecs05" value='networks' <?php if ( $ecs05 == "networks" ) { echo "checked"; } ?> > <label for="ecs05">Networks</label> <input type="checkbox" name="ecs06" id="ecs06" value='university-and-colleges' <?php if ( $ecs06 == "university-and-colleges" ) { echo "checked"; } ?> > <label for="ecs06">University and Colleges</label> </fieldset> <button id="search-submit" class="button">Apply Filters</button> </form>
Note: the ‘Get’ method shows the string in the URL useful for copy and paste and the ‘post’ method hides the string which maybe useful if you want to have a cleaner URL.