使我的定制列可排序

时间:2016-07-09 作者:Roy Savion

我正在将自定义帖子类型与ACF的关系字段一起使用,并尝试获取帖子中使用“位置”的次数列(这是我的自定义帖子类型-位置)。

除了可排序选项之外,我的一切都在工作,但我似乎弄不清楚。

如果您有任何帮助,我们将不胜感激,以下是我目前的代码:

function change_columns( $cols ) {
  $cols = array(
    \'cb\'       => \'<input type="checkbox" />\',
    \'title\'      => __( \'Title\',      \'trans\' ),
    \'number\'      => __( \'Number\',      \'trans\' ),
    \'author\'      => __( \'Author\',      \'trans\' ),
    \'Date\'      => __( \'Date\',      \'trans\' ),  
  );
  return $cols;
}
add_filter( "manage_edit-usa-locations_columns", "change_columns" );

function columns_content_only_locations($column_name, $post_ID) {
    if ($column_name == \'number\') {

    $locations = get_posts(array(
        \'post_type\' => \'post\',
        \'category\'  => \'3\',
        \'numberposts\'=>-1,
        \'orderby\' => \'title\',
        \'posts_per_page\'   => -1,
        \'meta_query\' => array(
            array(
                \'key\' => \'headquarters\', // name of custom field
                \'value\' => \'"\' . get_the_ID() . \'"\', // matches exaclty "123", not just 123. This prevents a match for "1234"
                \'compare\' => \'LIKE\'
            )
        )
    ));

    $totalCount = count( $locations );
    echo $totalCount;

    }
}

add_action(\'manage_usa-locations_posts_custom_column\', \'columns_content_only_locations\', 10, 2);

// Make these columns sortable
function sortable_columns() {
  return array(
    \'number\'      => \'number\',
    \'title\'      => \'title\',
  );
}

add_filter( "manage_edit-usa-locations_sortable_columns", "sortable_columns" );


function number_column_order( $vars ) {
    if ( isset( $vars[\'orderby\'] ) && \'count\' == $vars[\'orderby\'] ) {
        $vars = array_merge( $vars, array(
            \'meta_key\' => \'count\',
            \'orderby\' => \'meta_value_num\'
        ) );
    }

    return $vars;
}
add_filter( \'request\', \'number_column_order\' );

1 个回复
SO网友:mmm

在行动中request, 钥匙meta_keyorderby 已存在,请尝试这样覆盖:

if ( isset( $vars[\'orderby\'] ) && \'count\' == $vars[\'orderby\'] ) {

    $vars["meta_key"] = "count";
    $vars["orderby"] = "meta_value_num";

}

相关推荐

MANAGE_USERS_CUSTOM_COLUMNS筛选器不触发回调函数

我使用以下函数向users表中添加了一些自定义列 public function modify_users_columns( $column_headers ) { //Remove email, role, and posts column unset( $column_headers[\'email\'], $column_headers[\'role\'], $column_headers[\'posts\'] ); //Add sponsor