Using HTML drop down boxes to sort through custom fields in WordPress
I have a website (located here) an开发者_如何学Pythond I would like to do something to improve the way people search and view different reviews.
I have thought about introducing a way to search through posts based on rating. I currently have one category which has custom field with the field title "Rating". Each value in the custom field is an integer from 1 to 5.
Here's the test category (http://enoda.co.uk/category/ipad-2/ipad-reviews/ipad-utilities/)
I was wanting to know how I would go about introducing a drop down menu somewhere above the posts in the category with star ratings from 1 up to 5. Further, I would like it to take users to reviews with that rating.
Any help would be great,
Best regards, JackJack,
This is a somewhat complex issue as there are a number of areas within your WP installation that you need address to solve your problem. I'll point you in the right direction, but I don't anticipate that this will be the full answer for you.
First of all, the dropdown. Since you know that you want nothing more that a dropdown that displays choice of rating, you can easily hard code this. Since it is in a custom field, controlled by you, that only you/your colleagues set, you will know what is in that field and can feel safe hard coding this. Alternatively, I would have set up rating as a custom taxonomy with the terms '1 Star', '2 Stars', etc. and then you could build your drop down dynamically from this taxonomy. But, since it's in a custom field, I would just use something like this:
<form action="rating-search.php" method="GET">
<select name="rating-search" id="rating-search">
<option value="1">1 Star</option>
<option value="2">2 Stars</option>
<option value="3">3 Stars</option>
<option value="4">4 Stars</option>
<option value="5">5 Stars</option>
</select>
<input id="rating-search-submit" name="rating-search-submit" value="Search" />
</form>
Now, I'll assume that you can handle the submission of this data. On the page that receives this data (you can accomplish this with a template page), you will need to modify the wp_query to search only for posts that have a meta value of rating = {input value}. You will need code similar to:
$query = new WP_Query();
$query->query(
array(
'meta_query' => array(
array(
'key' => 'rating', // Insert the name of the meta key here
'value' => $_POST['rating-search'], // This is the rating you are matching
'compare' => '=',
'type' => 'NUMERIC'
)
)
)
);
if($query->have_posts())
{
while($query->have_posts())
{
$query->the_post();
global $post;
//Do normal loop business here
}
}
That query will help you search for posts that have the custom field set to the desired value. Note that the meta_query
parameter only works for WP 3.1 and higher. It's a super powerful feature and worth updating to 3.1 alone. More about what you can do with the WP_Query class can be found here: http://codex.wordpress.org/Function_Reference/WP_Query.
I know you can't just copy and paste this code to get what you want, but I hope this gives you some ideas of what you can do to get closer to your desired result. Good luck!
精彩评论