1

I would like to learn the best way to filter custom post types by custom fields.

In the example I am working on, I use custom taxonomies to organize my posts and filter them.

I would like to add the functionality that would allow a user to filter posts by price and bedrooms in the ascending and descending order.

enter image description here

Price and Bedrooms are custom fields.

What is the best way to achieve this?

  • What had you tried and how far you got? What specific issues you hit trouble with? – Rarst Oct 11 '15 at 17:32
1

Since you have not provided any code, below code snippet assumes a 'property' as post type and price as meta field. I think the code will provide you some direction and you can take it from there. Feel free to make changes as needed:


$args = array('post_type' => 'property',
              'meta_key' => 'price',
              'orderby' => 'meta_value',
              'order' => 'ASC',
              'meta_query' => array(array('key' => 'price','compare' => '=',))
              );
$query = new WP_Query($args);
  • LIKE comparison is slow and is typically for string comparison. Using = will be much faster. – Milo Oct 11 '15 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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