• 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)
  • You will need to provide more detail about what you mean by this – specifically:
    > This is what I did till now (the query doesn’t filter correctly).
    How does it not filter properly?
    Errors? Warnings? Nothing? Wrong data?

    You’re not doing anything here in this loop:

    // Loop
        if($query->have_posts()):
            while( $query->have_posts() ): $query->the_post();
    
            endwhile;
        endif;
    Moderator bcworkz

    (@bcworkz)

    Other than the loop not sending any output, your code appears to work correctly. I tested it on my site, but I had to change a couple things to fit my existing data: Post type changed to one of my CPTs. Meta key changed to one of mine. Form field values changed to match my meta values. Added a the_title(); in the loop.

    However, the clear filter link likely doesn’t work right. You need to conditionally add the meta_query array only when $_POST[‘my_status’] exists. That is unless every post has at least a meta value of an empty string for key ‘crb_attiva_nonattiva’. Posts with no such meta key value at all will not be returned by using an empty string value as an arg.

    You also still need to validate $_POST[‘my_status’] before using it in a query even though form values are pre-determined. An attacker can still send malicious data using that key. Since there are only two possible values, you only need to verify it’s one or the other.

    Thread Starter mariograsso87

    (@mariograsso87)

    Many thanks to both of you.

    @corrinarusso , when I write that the filter doesn’t filter correctly I mean that, choosing one of the two options from the dropdown, I’m always redirect to the same CTP single page, regardless the field value matches or not. And it’s true, my loop was doing nothing, but even adding the_title(); nothing changes.

    @bcworkz , how did you make it work? I added the_title(); at the loop as you did, but it still doesn’t work… I will deal with the clear filter issue and the validation after I figure out how to make things working.

    Moderator bcworkz

    (@bcworkz)

    I didn’t change anything related to code functionality other than the title, dropdown field values, and the meta key targeted. Mainly to fit the data I already had. Your code resides on a custom page template I use to test PHP code. When the page initially loads there is no output because there is no $_POST[‘my_status’] value (the clear filter condition). But when selecting an option, I get the expected results. No redirects.

    There does not appear to be an issue with the code’s basic functionality, other than clear filter and validation. Check the form tag’s action attribute value. Maybe there’s something odd about what the_permalink() outputs? Where did you place your code? A CPT archive template (archive-offerta_lavoro.php)? the_permalink() will not return an archive’s URL. Permalinks are for posts. Try hard coding the form tag’s action attribute value and the clear filter URL. I’m assuming the template is only for the one CPT’s archives. Naming the template archive-offerta_lavoro.php will ensure that.

    Thread Starter mariograsso87

    (@mariograsso87)

    Thanks again @bcworkz . I placed the code in the archive template (archive-offerta_lavoro.php), but I also tried to put it in another template page with same results.

    First of all, let me clarify that the query works. To be clear, if I try to loop the posts for ‘key’ => ‘crb_attiva_nonattiva’ and ‘value’ => ‘chiusa’, the posts are shown correctly. Ergo, I suppose the problem is with the dropdown filter.

    Coming to the problem with the permalink, I also tried to completely remove the clear filter button, but nothing seems to change…

    Moderator bcworkz

    (@bcworkz)

    Are you saying you are redirected to a single post after the archive page loads with the correct query results? There’s nothing in the code you posted that would do that. The only immediate issue I see is you cannot use the_permalink() to get an archive’s URL. That and needing to output something within the Loop. Validation and clearing can be dealt with later.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Create a dropdown filter with custom field values’ is closed to new replies.