Виведення для типу записів додаткової колонки з сортуванням по ній
Category: WordPress
Приклад коду нижче показує, як в панелі керування записами в WordPress вывести додаткову колонку.
Код створено для виведення ACF – поля “year”, для кастомного типу записів “fund”.
Для створення кастомного типу записів використано плагін Custom Post Type UI (розробник WebDevStudios).
// Create a new column in the admin panel add_filter('manage_fund_posts_columns', 'add_year_column', 4); function add_year_column( $columns ){ // Delete the NAME column (if necessary) // unset($columns['NAME']); // Insert column (3rd column, right after TITLE) $out = array(); foreach($columns as $col=>$name){ if(++$i==3) $out['year'] = 'Year'; $out[$col] = $name; } return $out; } // Filling the columns with information // (wp-admin/includes/class-wp-posts-list-table.php) add_filter('manage_fund_posts_custom_column', 'fill_year_column', 5, 2); function fill_year_column( $colname, $post_id ){ if( $colname === 'year' ){ echo get_post_meta($post_id, 'year', 1); } } // Adding the ability to perform sorting add_filter('manage_edit-fund_sortable_columns', 'add_year_sortable_column'); function add_year_sortable_column($sortable_columns){ $sortable_columns['year'] = 'year_year'; return $sortable_columns; } // Editing the column sorting query add_filter('pre_get_posts', 'add_column_year_request'); function add_column_year_request( $object ){ if( $object->get('orderby') != 'year_year' ) return; $object->set('meta_key', 'year'); $object->set('orderby', 'meta_value_num'); }