Wordpress自定义列排序第一次不起作用

时间:2017-08-09 07:44:47

标签: php wordpress sorting

在wordpress中,我在“标签”列表页面中添加了“帖子计数”和“媒体计数”列。列添加正常。但是第一次点击时排序不起作用。

第二次点击,它开始正常工作。但随后“排序箭头”显示错误的方向。有谁能告诉我。为什么第一次排序不起作用?如果有人遇到同样的问题,请告诉我。这将是很大的帮助。

谢谢

以下是代码:

add_filter('manage_posts_columns', array($this, 'addCustomColumn'));
add_action('manage_post_tag_custom_column', array($this,'post_count_column'), 3, 3);
add_action('manage_post_tag_custom_column', array($this,'media_count_column'), 4, 3);
add_filter('manage_edit-post_sortable_columns', array($this,'post_column_register_sortable') );
add_action('pre_get_posts', array($this,'post_exclusive_orderby' ));


function addCustomColumn($postsColumns){
    $postsColumns = array_merge( $postsColumns,
        array('exclusive' => __('Exclusive')) );
    $postsColumns = array_merge( $postsColumns,
        array('wtf_tag' => __('Tags')) );
    $postsColumns = array_merge( $postsColumns,
        array('post_count' => __('Post Count')) );
    $postsColumns = array_merge( $postsColumns,
        array('media_count' => __('Media Count')) );

    $postsColumns = $this->replaceColumn('tags','wtf_tag',$postsColumns);
    return $postsColumns;
}

function post_count_column($value, $column_name, $id) {

    if( $column_name == 'post_count' ) {
        global $wpdb;

        $term = get_tag($id);
        $tag = urlencode($term->name);

        $qry = "SELECT COUNT(*) as post_count FROM $wpdb->posts
                                INNER JOIN $wpdb->term_relationships
                                ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
                                INNER JOIN $wpdb->term_taxonomy
                                ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
                                WHERE $wpdb->posts.post_type = 'post'
                                AND $wpdb->term_taxonomy.term_id = {$id};";

        $res = $wpdb->get_row($qry, ARRAY_A);

        echo '<a href="'.site_url().'/wp-admin/edit.php?tag='.$tag.'">'.$res['post_count'].'</a>';
    }
}

function media_count_column($value, $column_name, $id) {

    if( $column_name == 'media_count' ) {
        global $wpdb;

        $term = get_tag($id);
        $tag = urlencode($term->name);

        $qry = "SELECT COUNT(*) as media_count FROM $wpdb->posts
                                INNER JOIN $wpdb->term_relationships
                                ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
                                INNER JOIN $wpdb->term_taxonomy
                                ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
                                WHERE $wpdb->posts.post_type = 'attachment'            
                                AND $wpdb->term_taxonomy.term_id = {$id};";

        $res = $wpdb->get_row($qry, ARRAY_A);

        echo '<a href="'.site_url().'/wp-admin/upload.php?tag='.$tag.'&post_type=attachment">'.$res['media_count'].'</a>';
    }
}

function post_column_register_sortable($columns){
    $columns['post_count'] = 'post_count';
    $columns['media_count'] = 'media_count';
    return $columns;
}

function post_exclusive_orderby( $query ) {
    if( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby');
    if( 'exclusive' == $orderby ) {
        $query->set('orderby','meta_value');
        $metaQuery = array(
            'relation'=>'or',
            array(
                'key'     => 'is-exclusive-post',
                'compare' => 'NOT EXISTS',
            ),
            array(
                'key'     => 'is-exclusive-post',
                'value'   => array( 0, 1 ),
                'compare' => 'IN'
            )
        );
        $query->set('meta_query',$metaQuery);
    }
    elseif( 'media_count' == $orderby ) {
        $query->set('meta_key','media_count');
        $query->set('orderby','meta_value_num');
    }
    elseif( 'post_count' == $orderby ) {
        $query->set('meta_key','post_count');
        $query->set('orderby','meta_value_num');
    }
}

0 个答案:

没有答案