Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress get unique Custom Field values only
    primarykey
    data
    text
    <p>I am working on a Wordpress site that displays a number of Projects stored in Custom Post Types. Each Custom Post includes two Custom Fields (amongst others) "Project Name" and "Project Color". There may be several Custom Posts about a particular "Project Name".</p> <p>I want to create a legend (a key) of all the projects which lists the "Project Names" and "Project Colors" taken from the custom field values above.</p> <p>So for example, I have 3 projects called "Bananas" whose color is "Yellow", 6 called "Oranges" whose color is "orange" and 4 called "Apples" whose color is "green". My legend (key) should display one banana in yellow, one orange in orange and one apple in green. </p> <p>Of course if I use the loop it displays 3 bananas, 6 oranges and 4 apples. How can I display just a list of the Project Names and Project Colors?</p> <p>I've looked at three methods, a custom taxonomy, checking for duplicates in an array and using <a href="http://codex.wordpress.org/Custom_Queries" rel="nofollow"><code>GROUP BY</code></a> as a filter on <code>posts_groupby</code>. </p> <p><code>GROUP BY</code> seems like the right way to go but I can't get it to do anything. The furthest I got was putting this in my functions.php...</p> <pre><code>function sort_legend ( $groupby ) { global $wpdb; $groupby .= "{$wpdb-&gt;postmeta}.meta_key = 'Color' "; return $groupby; } </code></pre> <p>...and applying it as a filter around my Query object...</p> <pre><code>&lt;?php add_filter('posts_groupby', 'sort_legend'); ?&gt; &lt;?php if ( $myQuery-&gt;have_posts() ) while ($myQuery-&gt;have_posts() ) : $myQuery-&gt;the_post(); ?&gt; &lt;?php remove_filter('posts_groupby', 'sort_legend'); ?&gt; </code></pre> <p>...but I still have multiple entries.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload