I added a custom field (location) for some posts. I want to get all the locations from posts from a specific category and store them in an array. I am using the following code to get all the values:

$places = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM 
$wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );

Then, I want to display them in a dropdown list:

if ($places) {
    foreach ($places as $place) {
      echo "<option value=\"" . $place . "\">" . $place . "</option>";

However, it is important to get only the values that are related to a specific category. Currently, I am getting all the locations and I am not checking if the location is for a specific post category. I am using the defalt post types. I have very limited knowledge of sql and I do not have any idea what to do. Please help.

Your Answer

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

Browse other questions tagged or ask your own question.