I'm focusing on a Wordpress site that shows numerous Projects saved in Custom Publish Types. Each Custom Publish includes two Custom Fields (among others) "Project Title" and "Project Color". There might be several Custom Posts in regards to a particular "Project Title".

I wish to produce a legend (a vital) of all of the projects which lists the "Project Names" and "Project Colors" obtained from the custom area values above.

So for instance, I've 3 projects known as "Bananas" whose color is "Yellow", 6 known as "Oranges" whose color is "orange" and 4 known as "Apples" whose color is "eco-friendly". My legend (key) should display one blueberry in yellow, one orange in orange and something apple in eco-friendly.

Obviously basically make use of the loop it shows 3 bananas, 6 oranges and 4 apples. How do i display just a listing from the Project Names and Project Colors?

I have checked out three techniques, a custom taxonomy, checking for replicates in a wide array and taking advantage of [cde] like a filter on GROUP BY.

posts_groupby appears like the proper way to go however i can't have it to complete anything. The farthest I acquired was putting this during my functions.php...


...and using it as being a filter around my Query object...

function sort_legend ( $groupby ) {
    global $wpdb;
    $groupby .= "{$wpdb->postmeta}.meta_key = 'Color' "; 
    return $groupby;

...however i have multiple records.

Here's my working solution...

Remove the 'Project Colors' and 'Project Names' custom fields in the custom template, (sigh), register a custom taxonomy known as 'Project Classes' and assign the recently registered taxonomy towards the custom publish type, see here. Within the Wordpress interface, give each custom publish its appropriate 'Project Class' bananas, apples etc, again described within the above link.

However there's still the issue of methods to exhibit one key for every custom taxonomy entry. I used again the Project Class names as style sheet names and so i could give each style title experience-color. Nevertheless the real Project Class names have capital letters and spaces which have to be removed to ensure they are style sheet friendly.

Give a php function <?php add_filter('posts_groupby', 'sort_legend'); ?> <?php if ( $myQuery->have_posts() ) while ($myQuery->have_posts() ) : $myQuery->the_post(); ?> <?php remove_filter('posts_groupby', 'sort_legend'); ?> in functions.php that amends the spaces and capital letters within the 'Project Class' taxonomy title and causes it to be appropriate for any css style title, I made use of...


You can now make use of your Project Class names as style sheet names.

Next, inside your page template display your custom taxonomy thus...

// Rename Projects custom field for css styles
function rename_projects($sProjectName) {
$sFind = ' '; 
$sReplace = '-';
$sAmended = mb_strtolower(str_replace( $sFind, $sReplace, $sProjectName ));
return $sAmended;

Now for instance a <?php $args=array( 'taxonomy' => 'projectclass', 'orderby' => 'name', 'order' => 'ASC' ); $categories=get_categories($args); foreach($categories as $category) { $sColorName = rename_projects($category->name); // To lower case and replace spaces echo '<div class="legend-color ' . $sColorName . '"></div> // Color style name <div class="legend-project">' . $category->name . '</div>'; // Taxonomy name } ?> "My First Project" includes a corresponding style sheet title $category->name which you'll supplment your style sheet and elegance. Observe that within the my-first-project I'm adding the design and style title towards the existing class therefore the resulting class is going to be "legend-color my-first-project": i.e. two styles. Hard coded class "legend-color" holds all of the style characteristics except background-color that is in <div class="legend-color '.

Working fine here.