This is a follow-on from this thread: http://wordpress.org/support/topic/how-to-display-the-number-of-posts-under-each-tag
The website I produced is here:
The background is that I've created a sort of index page which:
- Loops through all posts looking for tags (eg persons' names)
- Prints the tags in a list
- Next to each tag, prints a comma separated list of excerpts from each of the posts tagged with that tag
The live page is here: http://thedublinreview.com/contributors/
The code that produces this is here:
<?php
$tags_and_posts = $wpdb->get_results("
SELECT tr.object_id,p.post_excerpt,p.guid,t.name
FROM wp_term_relationships tr
JOIN wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
JOIN wp_posts p ON tr.object_id = p.ID
JOIN wp_terms t ON tt.term_id = t.term_id
WHERE tt.taxonomy = 'post_tag'
AND p.post_status = 'publish'
AND p.post_type = 'post'
ORDER by t.name
");
$tag_array = array();
$letters = array();
foreach( $tags_and_posts as $unwanted_key => $result ) {
$tag_array[$result->name][] = '<a href="'. get_permalink( $result->object_id ) .'">' . $result->post_excerpt. '</a>';
}
foreach( $tag_array as $tag => $post_excerpts ) {
// Ignore
$letter = ( strpos( $tag, ' ' ) ) ? explode( ' ',$tag ) : $tag;
if( is_array( $letter ) ) {
$letter = array_reverse( $letter );
$surname = strtolower( $letter[0] );
$letter = $letter[0]{0};
}
else {
$surname = strtolower( $letter );
$letter = $letter{0};
}
$letters[ $letter ][ $surname ] = $tag . ' <td class="issue-numbers"> ' . implode( ' <span class="sep">/ </span>', $post_excerpts ) . ' </td> ';
}
ksort( $letters );
foreach( $letters as $tag_letter => $tags ) {
// Ignore
ksort( $tags );
// Create a list for each letter
print '<table id="index-table"><tr><td class="author-name">' . implode( '</td></tr><tr><td class="author-name">', $tags ) . '</td></tr></table>';
}
?>
My problem is the name Philip Ó Ceallaigh. This name gets alphabetized under "C", which is incorrect. Equally incorrect, though slightly more acceptable, is how I have the name listed now: Philip ÓCeallaigh (without the space between Ó and Ceallaigh), however this name gets alphabetized at the very bottom of the list after the "Z". I'm happy to stick with Philip ÓCeallaigh, but is there any way to alter the code so that "Ó" is alphabetized just after "O" and before "P"?