令人惊讶的是,Wordpress并没有让它变得非常简单。尽管很多人在wordpress上都有类似的问题。org论坛,我相信你的论坛是独一无二的,因为你想保持秩序和排序。
这绝不是高质量的代码,但它完全符合您的要求。
class WP_Query_Custom extends WP_Query {
static $filters = array( \'join\', \'groupby\', \'fields\', \'results\' );
function get_posts() {
$this->enable_filters();
$posts = parent::get_posts();
$this->disable_filters();
return $posts;
}
function enable_filters() {
foreach( self::$filters as $filter ) {
add_filter( \'posts_\' . $filter, array( __CLASS__, \'filter_\' . $filter ), 10, 1 );
}
}
function disable_filters() {
foreach( self::$filters as $filter ) {
remove_filter( \'posts_\' . $filter, array( __CLASS__, \'filter_\' . $filter ), 10, 1 );
}
}
function filter_join($join) {
global $wpdb;
return "{$join} LEFT JOIN {$wpdb->posts} AS post_attachment ON {$wpdb->posts}.ID=post_attachment.post_parent AND post_attachment.post_type=\'attachment\' ";
}
function filter_groupby($groupby) {
global $wpdb;
if ( ! empty($groupby) ) {
$groupby = ",$groupby";
}
return "{$wpdb->posts}.ID{$groupby}";
}
function filter_fields($fields) {
$fields = "GROUP_CONCAT(post_attachment.ID) AS post_attachments, ".$fields;
return $fields;
}
function filter_results($posts) {
$num_posts = count($posts);
$offset = 0;
$new_posts = array();
for ( $i = 0; $i < $num_posts; $i++ ) {
if ( ! empty($posts[$i]->post_attachments) ) {
array_push( $new_posts, $posts[$i] );
unset( $posts[$i] );
}
}
return array_merge( $new_posts, $posts );
}
}
$query = new WP_Query_Custom(\'post_type=post\');