I’m using custom post types to display properties and have a custom price field assigned to each post. On the property page I want to display a list of four similarly priced properties, two less than or equal to the price of the current property and two greater than the current price. Ordering properties numerically by price is easy enough with wp_query, but how do I only display the closest values to the price of the current property?
Leave a Reply
You must be logged in to post a comment.
One possibility is a direct SQL query similar to the one given here.
But I’m not convinced that it would be much more efficient than 2 queries, all handled by WordPress:
The is untested
Edit
Thanks for calling me out, it’s the only way to get better.
The updated query would look like this:
Thanks again, kaiser for making me do the right thing.
The quickest thing to do is to create your own query using
$wpdb
. The most basic function would look like this:Don’t forget to change the values of
meta_key
andpost_type
to fit your specific application. Hope this helps!