Filter results with custom field values and dropdown

Edited according to first comments and Pastebin code:

<?php /* You can also leave 'action' blank: action="" */ ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="my_size" id="size" class="postform" onchange="submit();">
        <option selected="selected">Choose a size</option>
        <option value="10">10</option>
        <option value="20">20</option>
</select>
</form>
<?php /* Reset filter */ ?>
<p><a href="https://wordpress.stackexchange.com/questions/83928/<?php the_permalink(); ?>">Clear filter</a></p>

<?php
if( !isset($_POST['my_size']) || '' == $_POST['my_size']) {

    // unfiltered product list here

}
else {

    // Get dropdown value
    // Don't forget to sanitize this!
    $size = $_POST['my_size'];

    // Create new query
    $query = new WP_Query( array(
        'post_type'=> 'product', // your CPT
        'post_status' => 'publish',
        'meta_key' => 'size',
        'meta_value' => $size, // Dropdown value
    ) );

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

    // reset query to default
    wp_reset_postdata();

}

See Codex for WP_Query reference.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)