我正在将自定义帖子类型与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\' );