Ok, thank you all for your good answers. Anyway, I need more flexibility, so I ended up not using the wp template auto-sugars...
This answer is therefore not strictly related to my own original question, but I think it can help people too.
I built a custom function that retrieves a set of custom posts form a specific custom taxonomy and an optional parent term. If parent is passed, returned posts will belong to direct parent term children. This is the code:
// $post_type: string
// $taxonomy: string
// $parent_term: string (slug) || int (term_id)
// retrieves posts of a specific post_type and taxonomy, sorted by term
function get_taxonomy_posts($post_type, $taxonomy, $parent_term = ''){
if(!$post_type || !$taxonomy) return false;
global $wpdb;
//if $parent_term is defined and is not an integer then should be a slug, so let's get its id
if(!empty($parent_term) && !is_int($parent_term)){
$parent_data = get_term_by('slug',$parent_term,$taxonomy);
$parent_term = $parent_data->term_id;
}
$term_ids = get_terms($taxonomy, array( 'parent' => $parent_term, 'fields' => 'ids' ));
if(is_wp_error($term_ids)) return false;
$term_ids_str = implode(',',$term_ids);
// NOTE: terms.term_order gets added to tables by this plugin:
// http://wordpress.org/plugins/taxonomy-terms-order/
$query = $wpdb->prepare(
"select p.ID, p.post_title, p.post_content, p.post_excerpt, t.term_id, t.term_order, t.name as term_name, t.slug as term_slug, tt.count
from " . $wpdb->prefix . "posts p
inner join " . $wpdb->prefix . "term_relationships rel on p.ID = rel.object_id
inner join " . $wpdb->prefix . "term_taxonomy tt on tt.term_taxonomy_id = rel.term_taxonomy_id
inner join " . $wpdb->prefix . "terms t on tt.term_id = t.term_id
where p.post_type = %s and p.post_status = %s and t.term_id in (" . $term_ids_str . ")
order by t.term_order, p.menu_order"
,$post_type
,"publish"
);
$records = $wpdb->get_results($query);
$posts_array = array();
foreach($records as $record){
$term_name = $record->term_name;
if(!key_exists($term_name, $posts_array)) $posts_array[$term_name] = array();
$posts_array[$term_name][] = $record;
}
return $posts_array;
}//end get taxonomy_posts
It will give you an array where keys are term names and values are arrays of posts (or more precisely, post data of posts) belonging to that term. It can be easily modified to have for example keys as term_id instead of term_name. The single post data returned is like this:
object(stdClass)#2308 (9) {
["ID"]=>
string(3) "269"
["post_title"]=>
string(26) "title"
["post_content"]=>
string(0) "content"
["post_excerpt"]=>
string(0) "excerpt"
["term_id"]=>
string(3) "803"
["term_order"]=>
string(1) "2"
["term_name"]=>
string(27) "term name"
["term_slug"]=>
string(26) "term-slug"
["count"]=>
string(1) "3"
}
SIDE NOTE 1: currently, I'm using the taxonomy-terms-order plugins and so in the query I'm ordering terms by the custom term_order field. Again, if you don't use the plugin, just change the fields retrieved and order by clause as you prefer.
SIDE NOTE 2: Instead of a taxonomy-taxonomyname-termname.php, now I basically use a dummy page and apply a custom template (I mean /* Template Name: [name] */
): this is not mandatory, but I do it to have the wp's main query just do a simple 1 post query instead a sort of duplicated term query.
Hope it helps!