add coauthors_filter

This commit is contained in:
Profile Profile
2026-02-03 22:56:39 +03:00
parent 61b86cdca5
commit 141d117b96
2 changed files with 217 additions and 0 deletions

View File

@@ -0,0 +1,209 @@
<?php
// Убираем стандартный фильтр авторов для profile_article
add_action('admin_head-edit.php', 'cap_hide_default_author_filter');
function cap_hide_default_author_filter() {
global $typenow;
if ($typenow === 'profile_article') {
echo '<style>
select[name="author"] { display: none !important; }
/* Фиксируем расположение фильтров в одну строку */
.tablenav .actions {
display: flex !important;
align-items: center !important;
flex-wrap: nowrap !important;
gap: 5px !important;
}
.tablenav .actions > * {
flex-shrink: 0 !important;
margin: 0 !important;
}
#author-filter-wrapper {
display: inline-flex !important;
align-items: center !important;
gap: 3px !important;
flex-shrink: 0 !important;
}
#author_name_filter {
width: 140px !important;
}
.select2-container {
flex-shrink: 0 !important;
}
#cap-filter-submit {
flex-shrink: 0 !important;
white-space: nowrap !important;
}
</style>';
}
}
// Добавляем наш фильтр по Co-Authors Plus
add_action('restrict_manage_posts', 'cap_filter_by_coauthor');
function cap_filter_by_coauthor($post_type) {
if ($post_type !== 'profile_article') {
return;
}
global $wpdb;
$authors = $wpdb->get_results("
SELECT DISTINCT t.term_id, t.name, t.slug
FROM {$wpdb->terms} t
INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
INNER JOIN {$wpdb->term_relationships} tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
INNER JOIN {$wpdb->posts} p ON tr.object_id = p.ID
WHERE tt.taxonomy = 'author'
AND p.post_type = 'profile_article'
AND p.post_status = 'publish'
ORDER BY t.name ASC
");
if (!empty($authors)) {
$current = isset($_GET['author_name']) ? $_GET['author_name'] : '';
// Обертка для фильтра и кнопки
echo '<div id="author-filter-wrapper">';
echo '<select name="author_name" id="author_name_filter">';
echo '<option value="">Все авторы</option>';
foreach ($authors as $author) {
$coauthor = get_user_by('slug', $author->slug);
if ($coauthor) {
$display_name = $coauthor->display_name;
} else {
global $coauthors_plus;
if ($coauthors_plus) {
$guest = $coauthors_plus->get_coauthor_by('user_nicename', $author->slug);
$display_name = $guest && !empty($guest->display_name) ? $guest->display_name : $author->name;
} else {
$display_name = $author->name;
}
}
printf(
'<option value="%s" %s>%s</option>',
esc_attr($author->slug),
selected($current, $author->slug, false),
esc_html($display_name)
);
}
echo '</select>';
// Кнопка внутри обертки
submit_button('Фильтр', '', 'filter_action', false, ['id' => 'cap-filter-submit']);
echo '</div>';
}
}
// Модифицируем запрос для фильтрации
add_filter('parse_query', 'cap_filter_posts_by_coauthor');
function cap_filter_posts_by_coauthor($query) {
global $pagenow, $typenow;
if (!is_admin() || $pagenow !== 'edit.php' || $typenow !== 'profile_article') {
return;
}
if (isset($_GET['author_name']) && !empty($_GET['author_name'])) {
$query->query_vars['tax_query'] = [
[
'taxonomy' => 'author',
'field' => 'slug',
'terms' => sanitize_text_field($_GET['author_name'])
]
];
}
}
// Подключаем Select2 и стили
add_action('admin_enqueue_scripts', 'cap_enqueue_select2');
function cap_enqueue_select2($hook) {
if ($hook !== 'edit.php') {
return;
}
global $typenow;
if ($typenow !== 'profile_article') {
return;
}
wp_enqueue_style('select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css');
wp_enqueue_script('select2', 'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js', ['jquery'], null, true);
wp_add_inline_script('select2', '
jQuery(document).ready(function($) {
// Удаляем дублированные обертки
$("#author-filter-wrapper").not(":first").remove();
var $select = $("#author_name_filter").first();
$select.select2({
placeholder: "Все авторы",
allowClear: true,
width: "140px"
});
// Обработка очистки
$select.on("select2:clear", function(e) {
$(this).val("").trigger("change");
$(this).closest("form").submit();
});
// Обработка выбора
$select.on("select2:select", function(e) {
$(this).closest("form").submit();
});
// Enter в поиске
$(document).on("keypress", ".select2-search__field", function(e) {
if (e.which === 13) {
e.preventDefault();
$select.closest("form").submit();
}
});
});
');
wp_add_inline_style('select2', '
.select2-container--default .select2-selection--single {
height: 32px;
border-color: #8c8f94;
border-radius: 3px;
background: #fff;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
line-height: 30px;
color: #50575e;
padding-left: 8px;
font-size: 13px;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
height: 30px;
}
.select2-container--default.select2-container--focus .select2-selection--single {
border-color: #2271b1;
box-shadow: 0 0 0 1px #2271b1;
}
.select2-dropdown {
border-color: #8c8f94;
}
.select2-container--default .select2-results__option--highlighted[aria-selected] {
background-color: #2271b1;
}
.select2-container--default .select2-search--dropdown .select2-search__field {
border-color: #8c8f94;
font-size: 13px;
}
');
}