1

I have a whole list of directors but would like to display only their unique names not duplicates. Something that looks like

director A, Director B, Director C,...

NOT

director A, Director A, Director B, Director C, Director C,...

I try to do this with array_unique but it doesn't seem to put any data in the arrays.

I see that the foreach loop displays all the names of the directors but somehow the array $alldirectors stays empty.

Here is the code I'm using.

<?php
$resume = get_posts(array(
          'post_type' =>'resume', 
          'numberposts'=>-1, 
          'meta_key' => 'director', 
          'meta_value' => '' 
));

$alldirectors = array();
foreach( $resume as $post ) {
      $director = get_post_meta( $post->ID, 'director', true );
 }

 $directors = array_unique($alldirectors);
 foreach ($directors as $director) {
  echo $directors;
 }
 ?>

It's probably something simple that I'm missing but I'm new to php and wordpress Thanks for all your help.

Phil
  • 561
  • 2
  • 16
  • 29

3 Answers3

11

Try to use standard query like this

global $wpdb; (this is required when are you inside the function)

$values = $wpdb->get_results("SELECT DISTINCT meta_value FROM $wpdb->postmeta pm, $wpdb->post p WHERE meta_key  = 'director' and pm.post_id=p.ID  and p.post_type='resume' ",ARRAY_A);

print_r($values);
Sumit
  • 698
  • 4
  • 11
  • 1
    I would agree with this approach. Just get the data directly from the database the way you want it rather than making a series of database queries (one for each post) using pre-defined functions in a loop. You are talking about making a single DB query with this approach rather than hundreds or thousands depending on the number of posts you have. – Mike Brant Nov 29 '13 at 21:23
  • I agree with what Mike said. Do make note of this in your comments or somewhere visible so the people maintaining your setup will know, as - in theory - the db structure may change (if it does, though, it's likely we would all become very busy). – Herbert Van-Vliet Mar 05 '18 at 01:11
  • 2
    Thanks for your answer. I would correct one point though : `$wpdb->post` should be `$wpdb->posts`. – Pipo Jul 11 '19 at 14:34
  • 2
    To note: when WordPress queries `get_posts`, it grabs the meta data and puts it into memory, so there shouldn't be individual db requests with `get_post_meta`. – WraithKenny Jan 15 '20 at 15:13
1

You are not storing the directors name in $alldirectors therefore it is empty try this one

$alldirectors = array();
foreach( $resume as $post ) {
    $alldirectors[]=   get_post_meta( $post->ID, 'director', true );
 }

And then use your loop

 $directors = array_unique($alldirectors);
 foreach ($directors as $director) {
  echo $directors;
 }
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • 1
    I totally missed out on the [] behind the $alldirectors. Once I've put this it all worked as a charm. – Phil Nov 29 '13 at 21:40
0

array_unique will do what you want it to. However looking at your code you have not assigned any data to the $alldirectors array. Try this line:

$alldirectors = get_post_meta(blah blah); 

instead of

$director = get_post_meta(blah blah);
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
James Jones
  • 3,850
  • 5
  • 25
  • 44