I know there are many questions asking the same thing, but i get lost when it comes about functions. I am using a php function stored in wordpress functions.php which i can call from template in order to get inside a dropdown all the data stored in a taxonomy
function custom_taxonomy_dropdown( $taxonomy, $exclude = array(''), $name,
$show_option_all = null )
{
$args = array(
'exclude' => $exclude,
'order' => $order,
'hide_empty' => 1,
'order' => 'ASC',
'echo' => 1
);
$terms = get_terms( $taxonomy, $args );
$name = ( $name ) ? $name : $taxonomy;
if ( $terms ) {
printf( '<select name="%s" class="postform">', esc_attr( $name ) );
if ( $show_option_all ) {
printf( '<option value="0">%s</option>', esc_html( $show_option_all ) );
}
foreach ( $terms as $term ) {
printf( '<option value="%s">%s</option>', esc_attr( $term->slug ),
esc_html( $term- >name ) );
}
print( '</select>' );
}
}
Template file where i call the function:
// Other func to get the ids of tags and categories to exclude in the dropdown; irrelevant
$noticias_ID = get_category_id('Noticias');
$estrenos_ID = get_tag_id('estrenos');
$proximamente_ID = get_tag_id('proximamente');
$peliculas_ID = get_tag_id('peliculas');
// Calling the function of above and writing parameters about taxonomies
custom_taxonomy_dropdown( 'category', ''.$noticias_ID, 'category', 'Select All' );
custom_taxonomy_dropdown( 'actor', '', 'actor', 'Select All' );
custom_taxonomy_dropdown( 'director', '', 'director', 'Select All' );
custom_taxonomy_dropdown( 'escritor', '', 'escritor', 'Select All' );
custom_taxonomy_dropdown( 'post_tag',
''.$estrenos_ID.','.$proximamente_ID.','.$peliculas_ID.','.$pending_ID.','.$pendiente_ID, 'post_tag', 'Select All' );
So, i get some pretty selectable dropdowns with all the taxonomy information. However, how can i pass the selected data in each of the dropdown to the submit action and therefore, click the submit button and get the correct results in my already done search.php?
<form action="<?php echo home_url('/'); ?>" method="get" class="">
<input type="submit" value="Submit">
</form>
EDIT: I have obtained the value for every select with $_GET
and stored it on a variable like $value_get
($category_get
, $director_get
, etc...)
if(isset($_GET["category"]))
{
$category_get = $_GET["category"];
}
if(isset($_GET["director"]))
{
$director_get = $_GET["director"];
}
if(isset($_GET["actor"]))
{
$actor_get = $_GET["actor"];
}
if(isset($_GET["escritor"]))
{
$escritor_get = $_GET["escritor"];
}
if(isset($_GET["post_tag"]))
{
$idioma_get = $_GET["post_tag"];
}
EDIT: So, by doing the last step i didn't knew if the next one was the query_posts($query_string)
, but anyways it didn't help me. Maybe i got confused here. Even when i made an echo of $query_string
the query was different and it even was showing the pagename among it. So, although it is the best way to do it, i decided not to go further in there because i have wasted much time on this problem. So, i went by the easy way and decided to pass the variable values obtained by the isset($_GET["taxonomy"]
to the $args
and query it. It all resulted like this:
$category_ID = get_category_id(''.$category_get); // Calling function
$idioma_ID = get_tag_id(''.$idioma_get); // to get ID's by name
$args = array(
'cat' => ''.$category_ID.'',
'category_name' => ''.$category_get.'',
'tag' => ''.$idioma_get.'',
'tag_in' => ''.$idioma_ID.'',
'director' => ''.$director_get.'',
'escritor' => ''.$escritor_get.'',
'actor' => ''.$actor_get.'',
);
The reason that i included both cat and category_name was because the two words terms, so it's like a fix. 'Select All'
option value must not be 0, as it will force all dropdowns to not stay on select all option but to select a different data. In my case, its value wasn't 0 it simply was: ''
So, for future references, this is my problem already solved. My solution does not use the query string but uses the values obtained by $_GET
which i can't consider completely right. The only answer made in this question helped me trough the problem but it didn't help me to pass the variables to the query, so i think i can't select a correct or complete answer here. If anyone else posts how to pass the correct data to $query_string
that will be the correct answer.