Title: Create form filter
Last modified: February 12, 2021

---

# Create form filter

 *  [dmunaretto](https://wordpress.org/support/users/dmunaretto/)
 * (@dmunaretto)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/create-form-filter/)
 * OK next big challenge. I have a page that generates matching properties, matching
   the criteria from the current property to other properties. I am basically setting
   a bunch of variables, then running the loop, then presenting the results, then
   closing the loop.
 * I want to use the following to create a basic filter on City –
 * [form action=]
    <i class=”fa fa-street-view” aria-hidden=”true”></i> I’m looking
   for an agent in [select name=filter] [for each=property-city]
 *  <option value=[each id]>[each]</option>
 *  [/for]
    [/select]
 *  [input type=submit]
    [/form]
 * I have used this before, but on a much simpler page.
 * Since I want this to display BEFORE the search results, I have this code before
   the loop occurs (otherwise I get one of these for every match). But I’m having
   a problem getting the filter to narrow the result set. I’ve basically used
 * [set var=…]
    [set var2=..]
 * [pass global=query fields=filter,etc,etc2,etc]
 * [loop]
    Run a bunch of [if]s to find matching results. [run a bunch of calculations,
   set a bunch of variables on results for the display of the content]
 * [if taxonomy property-city field=id value={FILTER}]
 * CONTENT DISPLAY CODE GOES HERE
 * [/if]
    [/loop]
 * Filter does show up in the URL as a parameter, but when I select a value from
   the dropdown list, it doesn’t filter the result. Also I can’t seem to figure 
   out how to set the OPTION SELECTED on the form itself based on the filter…
 * Any suggestions?
 * David

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Thread Starter [dmunaretto](https://wordpress.org/support/users/dmunaretto/)
 * (@dmunaretto)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/create-form-filter/#post-14043595)
 * Let me add that I would REALLY LIKE to be able to only show those taxonomy terms
   that had search results by using the [COUNT] feature, that would be the ultimate
   goal. I had an idea to do something like this — but since I’m building the form
   before the loop, not sure how to do it — maybe setup all items as variables then
   close the loop and generate the content display?
 *  [for each=property-city order=ASC current=true]
    [set validitem][each count][/
   set] [-if var=validitem compare=”>” value=0] [set taxtitle][field name][/set][/-
   if]
 * Then use this in the [form]…
 *  [nanny7](https://wordpress.org/support/users/nanny7/)
 * (@nanny7)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/create-form-filter/#post-14046775)
 * I am following this as I am after a similar thing.
 *  Thread Starter [dmunaretto](https://wordpress.org/support/users/dmunaretto/)
 * (@dmunaretto)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/create-form-filter/#post-14046810)
 * This works although it doesn’t limit the results to those taxonomy terms that
   are in the search results… I’m also having performance issues with it, but may
   just need to reorganize some things in my code —
 * BEFORE THE LOOP:
    [form action=] <i class=”fa fa-street-view” aria-hidden=”true”
   ></i> I’m looking for a home in: [select name=filter] [for each=property-city
   field=name order=ASC]
 *  <option value=[each id]>[each]</option>
 *  [/for]
    [/select]
 *  [input type=submit]
    [/form]
 * THIS IN THE LOOP BEFORE YOUR PRESENTATION / HTML STUFF –
 * [-pass global=_GET field=filter]
    [set taxfilter][taxonomy property-city field
   =id][/set] [–if var=taxfilter value='{-FIELD}’] [/note]
 * Hope it helps —
 * David
 *  Thread Starter [dmunaretto](https://wordpress.org/support/users/dmunaretto/)
 * (@dmunaretto)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/create-form-filter/#post-14046815)
 * What I’m trying to do is something that I can pass back to the form input so 
   that only the taxonomy terms that have results within the loop show up in the
   <select> options.
 * Was thinking something like this but can’t seem to make it work –
    [for each=
   property-city order=ASC current=true] [set validitem][each count][/set] [-if 
   var=validitem compare=”>” value=0] [set taxtitle][field name][/set] [/-if]
 *  [polarracing](https://wordpress.org/support/users/polarracing/)
 * (@polarracing)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/create-form-filter/#post-14047425)
 * This works like a charm:
 *     ```
       [for each=whatever current=true order=ASC]
       [set test][each count][/set]
       [if var=test value='0' compare=more]
       [each name]
       [/if]
       [/for]
       ```
   
 * Compare this to your code to see the difference:
 *     ```
       [for each=property-city order=ASC current=true]
       [set validitem][each count][/set]
       [-if var=validitem compare=”>” value=0] -> WHY A NESTED IF [-if] ?
       [set taxtitle][field name][/set] -> DO YOU HAVE A FIELD CALLED NAME?
       [/-if]
       ```
   
    -  This reply was modified 5 years, 2 months ago by [polarracing](https://wordpress.org/support/users/polarracing/).
    -  This reply was modified 5 years, 2 months ago by [polarracing](https://wordpress.org/support/users/polarracing/).
 *  Thread Starter [dmunaretto](https://wordpress.org/support/users/dmunaretto/)
 * (@dmunaretto)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/create-form-filter/#post-14053308)
 * My [-if] is nested only because it is running inside of another if statement 
   in my code, but obviously not needed to be that way otherwise.
 * – I have the form before the loop with a select called [filter] and each option
   value as [each id] from the taxonomy.
    – Then in the loop I’m passing [filter]
   to an [if] so that it only shows results that match the [filter].
 * This works, but what I really want to do is:
    1) Within the loop create a variable[
   validcities] for each property-city within the search results. 2) Pass that variable
   into the form select [filter] as each <option> in the form. 3) In the loop if
   a user sets a [filter] from the form, limit the results of the search to the 
   selected [filter].
 * I see how your code works, but not sure how I would pass back into the form…?
   I think your code would need to be something like this —
 * [for each=whatever current=true order=ASC]
    [set test][each count][/set] [if 
   var=test value=’0′ compare=more] [set validcity][each id][/set] [/if] [/for]
 * Then how do i pass back into the [form] —- something like this?
 * [pass global=_GET field=validcity]
    [form action=] <i class=”fa fa-street-view”
   aria-hidden=”true”></i> I’m looking for a home in: [select name=filter] [for 
   each=property-city field=name order=ASC] [set taxidlist][each id] [if var=validcity
   compare=”exists”] [-if var=taxidlist compare=”=” value='{VALIDCITY}’] <option
   value=[each id]>[each]</option> [/-if] [else] <option value=[each id]>[each]</
   option> [/for] [/select]
 * [input type=submit]
    [/form] [/pass]
    -  This reply was modified 5 years, 2 months ago by [dmunaretto](https://wordpress.org/support/users/dmunaretto/).
 *  [polarracing](https://wordpress.org/support/users/polarracing/)
 * (@polarracing)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/create-form-filter/#post-14053981)
 * You have to know the syntax.
 * `[if var=validcity compare=”exists”]` is wrong -> correct = `[if var=validcity]`
 * `[-if var=taxidlist compare=”=” value='{VALIDCITY}’]` is wrong -> correct `[-
   if var=taxidlist value='{VALIDCITY}’]`
 * `[set taxidlist][each id]` is wrong -> correct `[set taxidlist][each id][/set]`
 * As long your syntax is wrong the code will not run.
    -  This reply was modified 5 years, 2 months ago by [polarracing](https://wordpress.org/support/users/polarracing/).
 *  Thread Starter [dmunaretto](https://wordpress.org/support/users/dmunaretto/)
 * (@dmunaretto)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/create-form-filter/#post-14069454)
 * Just an update, I have the form working such that it filters the result set based
   on the city chosen from the list of all cities, but still haven’t been able to
   figure out how to get just those cities that are within the result set into the
   form <option>s…
 * Here’s the code that works, I know someone else was interested…
 * [pass global=query field=filter]
    [for each=property-city current=false] [set
   filtval][each id][/set] [-if var=filtval value='{FILTER}’] [set citylabel][each
   name][/set] (NOTE: I’M USING THIS ELSEWHERE IN CODE TO HEADING THE NAME OF SELECTED
   CITY.) [/-if] [/for]
 * [form action=]
    [b]<i class=”fa fa-street-view” aria-hidden=”true”></i>Show Matching
   Properties In:[/b] [select name=filter] <option value=””>Select a City</option
   > [for each=property-city field=name order=ASC]
 *  <option value=[each id][-if each_field=id each_value='{FILTER}’] selected[/-
   if]>[each]</option>
 *  [/for]
    [/select]
 *  [input type=submit]
    [/form] [/pass]

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Create form filter’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-content-shortcode_766976.svg)
 * [Custom Content Shortcode](https://wordpress.org/plugins/custom-content-shortcode/)
 * [Support Threads](https://wordpress.org/support/plugin/custom-content-shortcode/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-content-shortcode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-content-shortcode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-content-shortcode/reviews/)

 * 8 replies
 * 3 participants
 * Last reply from: [dmunaretto](https://wordpress.org/support/users/dmunaretto/)
 * Last activity: [5 years, 2 months ago](https://wordpress.org/support/topic/create-form-filter/#post-14069454)
 * Status: not resolved