WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The function below lists the custom fields as in this example:

  • Frank Capra
  • Alfred Hitchcock
  • Woody Allen
  • Woody Allen
  • Frank Capra
  • Pedro Almodóvar
  • Pedro Almodóvar

I introduce the values ​​in alphabetical order and without repetition. Eg:

  • Alfred Hitchcock
  • Frank Capra
  • Pedro Almodóvar
  • Woody Allen

This is the code:

$movie_reviews = get_posts('numberposts=-1&orderby=post_name&order=ASC');
foreach($movie_reviews as $post) : setup_postdata($post);
<?php $director = get_post_meta($post->ID, "director", $single = true);
if($director !== '') {
echo $director;
} ?>
<?php endforeach; ?>

Is this possible? Grateful for any help.

share|improve this question
up vote 2 down vote accepted

You could do a custom SQL query:

global $wpdb;

$query = "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'director' ORDER BY meta_value;";
$directors = $wpdb->get_results( $query );

foreach( $directors as $director ) :
    echo $director->meta_value;

one thing to note here though is that since you're not querying the associated posts, you could get values that belong to unpublished posts, if that's a concern.

share|improve this answer
Thank you @Milo. The result was this (no space between items): Alfred HitchcockFrank CapraPedro Almodóvar. Another question: You can add link for each item? example: <a href="/Alfred-Hitchcock/">Alfred Hitchcock</a>. – BóbGCA Sep 21 '12 at 4:49
@Milo provide the hard part. From here you need to adapt his foreach loop to show the desired markup. – helgatheviking Sep 21 '12 at 4:57
Hello @helgatheviking, I'm adapting my loop. Already solved the problem of spaces adding <br>: echo $director->meta_value . '<br />'; It is possible to obtain items that start for example with the letter "a"? – BóbGCA Sep 21 '12 at 5:25
Perfect @Milo. I'm adapting my loop, already added a link for each item, now just need to separate the results by letter order. I want to list only directors whose names begin with the letter "a". Any tips? – BóbGCA Sep 21 '12 at 6:15
@MichaelWilliam Please ask one question per topic. – kaiser Sep 21 '12 at 12:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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