I need to filter posts by a custom field value.

My specific example:

  • User browse to products > category 1 > subcategory A.
  • ALL "subcategory A" items (products custom post type) are displayed as default by date.
  • Each item in subcategory A has a custom field "size". User can select the size with a dropdown menu and posts are filtered displaying only items with "custom field:size" = "selected size". Filter is kept also in pagination, because products are on more pages.

I've checked a lot of answers, but no case was simple as that. I was thinking about doing it using a custom search or a plugin, but I really don't know where to start. thanks for your help.

EDIT: the custom field values are fixed, not auto-populated. The select can be built in plain html without extra queries.

1 Answer 1


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>
<?php /* Reset filter */ ?>
<p><a href="<?php the_permalink(); ?>">Clear filter</a></p>

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
        while( $query->have_posts() ): $query->the_post();
            // Product content

    // reset query to default


See Codex for WP_Query reference.

  • Thanks Max. I tried your solution but I'm stuck with the submit of the select, this is the code I used pastebin.com/t36UWQsP I don't know which "action" is correct for the form submitting on "self". If I understood it should append "&my_size=xx" at the end of url as a parameter, right? Your solution looks right, but my php knowledge is pretty basic, sorry.
    – Andycap
    Commented Feb 1, 2013 at 15:09
  • Andycap, please see amended code above.
    – Max Yudin
    Commented Feb 1, 2013 at 15:34
  • Andycap, you are welcome. I forgot to answer your question about URL parameters. When you use method="get", parameters are appended, when method="post" you don't see them in URL.
    – Max Yudin
    Commented Feb 1, 2013 at 17:12
  • Max, thanks for that instructive detail, I had already figured it out reading a tutorial on php GET and POST.
    – Andycap
    Commented Feb 2, 2013 at 12:40
  • I don't understand what happens but it's showing posts with "Size" and lower (i.e. size=10, it's showing 10,9,8, etc..). I also tried with 'compare' => '=' but nothing.
    – Andycap
    Commented Feb 2, 2013 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.