Pass a Select List Value into Functions.php function Tax Query Array
-
Hoping someone can assist as I am very new to the whole functions.php/wordpress coding side of things but I basically have the following use case that I am trying to achieve through the use of Gravity Forms plugin as well as some other coding.
Use Case: Present a user with a drop-down/select list with a variety of occasions, for example:
Using WordPress v3.7.1
<select name="input_1" id="input_1_1"> <option>Christening</option> <option>Birthday</option> <option>Wedding</option> </select>
I also have another drop-down/select list that based on what the user selects here, all media library images that have a tag, matching the selection above, will only be displayed in the drop-down list. FYI, I have also installed the plugin called **WordPress Media Tags** that has provided me the ability to tag all files within my media library.
Now, I have the process to filter the media library into my select list but at the moment I only know how to hard code the tax_query terms value, that I have in my child theme **functions.php** file, which looks like this:
<?php add_filter('gform_pre_render_2', 'populate_images'); function populate_images($form){ foreach($form['fields'] as &$field){ if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-images') === false) continue; $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'order' => 'ASC', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'media_tag', 'terms' => 'wedding', // want to pass value here as a parameter 'field' => 'slug', ) ) ); $loop = new WP_Query($args); $images = array(array('text' => '-- Select an Image --', 'value' => ' ')); foreach ( $loop->posts as $image) { $images[] = array('text' => $image->post_title, 'value' => wp_get_attachment_url( $image->ID )); } $field['choices'] = $images; } return $form; }
As you can see, I have hardcoded the tax_query array terms with the value of ‘wedding’, which will return to me all media files with the tag ‘wedding’. This I have working.
What I am after but unsure how to do, is that I want to pass as a parameter to my **populate_images** function, within functions.php, say $term, which will be the value selected from the Occasion’s select/drop-down above, so my new tax_query would in actual fact look like:
'tax_query' => array( array( 'taxonomy' => 'media_tag', 'terms' => $term, //want to pass value here as a parameter 'field' => 'slug', ) )
So, I am unsure how to link value from select drop-down, pass into populate_images as the parameter $term and then have the functions.php file perform a new Wp_Query based off $term parameter.
If the user, selects ‘birthday’ from drop-down, call populate_images with the parameter of ‘birthday’ and present to the user within the second drop-down, all the images with the tag of ‘birthday’.
I guess also, if there is a better means of performing what I am after, using Gravity Forms plugin above, pls let me know.
Hope the above makes sense and really hope someone can point me in the right direction.
Thanks.
- The topic ‘Pass a Select List Value into Functions.php function Tax Query Array’ is closed to new replies.