Last active
June 1, 2023 13:18
-
-
Save HDDen/a6f7118fb17a3dd6fd653288d0deb70f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Post modified date, post uri and post parent title | |
*/ | |
//создаем новую колонку | |
function hdden_manage_pages_columns($columns) | |
{ | |
$columns['post_modified'] = 'Дата редакт.'; | |
$columns['fullslug'] = 'URI'; | |
$columns['parentnode'] = 'Родитель'; | |
return $columns; | |
} | |
//заполняем | |
function hdden_manage_pages_custom_columns($column_name, $post_id) | |
{ | |
if ($column_name === 'post_modified') { | |
$post = get_post($post_id); | |
echo 'Изменено <br>' . get_the_modified_date('d.m.Y \в h:i:s'); //$post->post_modified; | |
} elseif ($column_name === 'fullslug') { | |
$post = get_post($post_id); | |
echo '/' . get_page_uri(); | |
} elseif ($column_name === 'parentnode') { | |
$post = get_post($post_id); | |
if (isset($post->post_parent) && $post->post_parent){ | |
echo '('.$post->post_parent.') ' . get_the_title($post->post_parent); | |
} else { | |
echo '–'; | |
} | |
} | |
} | |
//добавляем возможность сортировки | |
function hdden_manage_pages_sortable_columns($sortable_columns) | |
{ | |
$sortable_columns['post_modified'] = ['post_modified', true]; // true = desc | |
$sortable_columns['fullslug'] = ['fullslug', true]; | |
$sortable_columns['parentnode'] = ['parentnode', true]; | |
return $sortable_columns; | |
} | |
// включаем для других пост-тайпов | |
function hdden_cpt_modColumns() | |
{ | |
$post_types = array( | |
'page', 'post', | |
); | |
if ($post_types) { | |
foreach ($post_types as $cpt) { | |
add_filter('manage_' . $cpt . '_posts_columns', 'hdden_manage_pages_columns'); | |
add_action('manage_' . $cpt . '_posts_custom_column', 'hdden_manage_pages_custom_columns', 10, 2); | |
add_filter('manage_edit-' . $cpt . '_sortable_columns', 'hdden_manage_pages_sortable_columns'); | |
} | |
} | |
} | |
hdden_cpt_modColumns(); | |
// чиним сортировку | |
add_action( 'pre_get_posts', 'hdden_cpt_modColumns_orderby' ); | |
function hdden_cpt_modColumns_orderby( $query ) { | |
if( ! is_admin() ) | |
return; | |
// узнаём режим сортировки | |
$orderby = $query->get( 'orderby'); | |
// добавляем параметры в сортировку | |
if( 'parentnode' == $orderby ) { | |
$query->set('orderby','parent'); | |
} elseif( 'fullslug' == $orderby ) { | |
$query->set('orderby',array( | |
'parent' => 'ASC', | |
'name' => 'ASC', | |
)); | |
} | |
// пример с мета-полем | |
// if( 'slice' == $orderby ) { | |
// $query->set('meta_key','slices'); | |
// $query->set('orderby','meta_value_num'); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment