下面是我在某处找到并正在进行的查询。查询的目的是根据分类法对帖子进行排序。因此,我正在共享代码,以便它可以帮助您理解posts\\u子句的用法。
function color_clauses_mike( $clauses, $query ) {
global $wpdb;
if ( isset( $query->query[\'orderby\'] ) && \'color\' == $query->query[\'orderby\'] ) {
$clauses[\'join\'] .= <<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;
$clauses[\'where\'] .= " AND (taxonomy = \'color\' OR taxonomy IS NULL)";
$clauses[\'groupby\'] = "object_id";
$clauses[\'orderby\'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
$clauses[\'orderby\'] .= ( \'ASC\' == strtoupper( $query->get(\'order\') ) ) ? \'ASC\' : \'DESC\';
}
return $clauses;
}
add_filter( \'posts_clauses\', \'color_clauses_mike\', 10, 2 );
以下是我找到上述查询的来源:
http://scribu.net/wordpress/sortable-taxonomy-columns.html