0

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.

  • 1
    Somebody has recently asked [a similar question](https://stackoverflow.com/questions/70754902/how-to-filter-archive-posts-based-on-custom-taxonomy-in-wordpress/). Please take a look at the `query` I wrote for him/her. – Ruvee Jan 21 '22 at 14:23
  • Hi @Ruvee and thanks for your answer. I'm sure I'm missing something (I'm just a newbie), but I really don't understand how to adapt the code that you wrote answering the other question to my case... Can you please give me a hint? – mmm1987 Jan 22 '22 at 12:07

0 Answers0