// ----------------------------------------------------------------------------------------------
// 15 - RAZENÍ PODLE ZOBRAZENÍ V ADMINISTRACI
// ----------------------------------------------------------------------------------------------
add_action( 'init', 'admin_zobrazeni_sortable' );
function admin_zobrazeni_sortable() {
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
add_filter( 'manage_edit-' . $post_type . '_columns', 'zobrazeni_columns' );
add_action( 'manage_' . $post_type . '_posts_custom_column', 'zobrazeni_column_content', 10, 2 );
add_filter( 'manage_edit-' . $post_type . '_sortable_columns', 'sortable_zobrazeni_column' );
}
add_action( 'pre_get_posts', 'zobrazeni_orderby' );
function zobrazeni_columns( $columns ) {
$columns['zobrazeni'] = __( 'Zobrazení', 'hc' );
return $columns;
}
function zobrazeni_column_content( $column_name, $post_id ) {
if ( 'zobrazeni' != $column_name ) {
return;
}
$zobrazeni = get_post_meta( $post_id, 'count_page_hits', true );
echo intval( $zobrazeni );
}
function sortable_zobrazeni_column( $columns ) {
$columns['zobrazeni'] = 'zobrazeni';
return $columns;
}
function zobrazeni_orderby( $query ) {
if ( ! is_admin() ) {
return;
}
$orderby = $query->get( 'orderby' );
if ( 'zobrazeni' == $orderby ) {
$query->set( 'meta_key', 'count_page_hits' );
$query->set( 'orderby', 'meta_value_num' );
}
}
}