Create a dropdown filter with custom field values
-
This is my situation: I created a CPT called offerta_lavoro. Within the post, there’s a custom field, created using select Carbon Fields, called crb_attiva_nonattiva. This field has two options, “aperta” or “chiusa”.
I’m trying to create an archive page with a dropdown filter that allows the user to filter contents that contain the value “aperta” or “chiusa”.
This is what I did till now (the query doesn’t filter correctly).
<form method="post" action="<?php the_permalink() ?>"> <select name="my_status" id="stato" class="postform" onchange="submit();"> <option selected="selected">Choose a status</option> <option value="aperta">Aperta</option> <option value="chiusa">Chiusa</option> </select> </form> <?php /* Reset filter */ ?> <p><a href="<?php the_permalink(); ?>">Clear filter</a></p> <?php if( !isset($_POST['my_status']) || '' == $_POST['my_status']) { } else { $stato = $_POST['my_status']; // Create new query $query = new WP_Query( array( 'post_type'=> 'offerta_lavoro', // your CPT 'post_status' => 'publish', 'meta_query'=>array( array( 'key' => 'crb_attiva_nonattiva', 'value' => $stato, ), ), ) ); // Loop if($query->have_posts()): while( $query->have_posts() ): $query->the_post(); endwhile; endif; // reset query to default wp_reset_postdata(); } ?>
Where is my mistake?
Thanks in advance, I hope my explanation is clear enough.
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Create a dropdown filter with custom field values’ is closed to new replies.