mercredi 25 février 2015

Query postmeta values, and return multiple post_titles for common meta value


As of now I query the DB:



global $wpdb;

$query = $wpdb->prepare('
SELECT p.post_id, p.meta_value, pm.post_title
FROM %1$s AS p
JOIN %2$s AS pm ON (p.post_id = pm.ID)
WHERE p.meta_key = "Price"',

$wpdb->postmeta,
$wpdb->posts
);
$results = $wpdb->get_results($query);


This returns an array of objects containing the metavalue, post_id, and post_title. Using these I create my dropdown of "prices"



<select name="s">
<?php

foreach ($results as $result) {

echo "<option value=\"$result->post_title\">$result->meta_value</option>";
}
?>
</select>


However, as of now this generates a selection for each value with no regard to weather it is a duplicate or not. For instance if price is "10" on two separate posts, I will have 10 twice in my dropdown. How can I alter this so that there is only 1 "10" and it returns all the posts with metavalue of 10?





Aucun commentaire:

Enregistrer un commentaire