Answer the question
In order to leave comments, you need to log in
How to sort by WordPress custom field for number of characters?
I use a WP plugin to count and display the number of characters of posts https://wordpress.org/plugins/posts-character-coun...
Here is the php file
<?php
class Posts_Character_Count_Admin {
public static function init() {
$plugin = new self();
add_action( 'admin_init', array( $plugin, 'admin_init' ) );
}
public function admin_init() {
// Only run our customization on the 'edit.php' page in the admin
add_action( 'load-edit.php', array( $this, 'admin_edit_screens' ) );
// Only run our customization on the 'post.php' page in the admin
add_action( 'load-post.php', array( $this, 'admin_post_screens' ) );
}
// Добавить сортировку по количеству знаков в столбце, чтобы всем управлять должностей экранов в том числе страниц
public function admin_edit_screens() {
// Add the character count column to all manage posts screens except pages
add_filter( 'manage_posts_columns', array( $this, 'admin_edit_columns' ) );
add_action( 'manage_posts_custom_column', array( $this, 'admin_edit_column_values' ), 10, 2 );
// Add the character count column to the manage pages screen
add_filter( 'manage_pages_columns', array( $this, 'admin_edit_columns' ) );
add_action( 'manage_pages_custom_column', array( $this, 'admin_edit_column_values' ), 10, 2 );
}
// Add the character count to the edit screens of all post types including pages
public function admin_post_screens() {
add_action( 'admin_footer', array( $this, 'post_edit_screen_admin_footer' ) );
}
/* Methods and Filters for the column in the Manage Posts/Pages SubPanel */
public function admin_edit_columns( $columns ) {
$columns['count'] = __( 'Character Count', 'posts-character-count-admin' );
return $columns;
}
public function admin_edit_column_values( $column, $post_id ) {
global $post;
if ( 'count' == $column ) {
$stat = new Posts_Character_Count( $post->post_content );
echo $stat->count_characters() . ' ' . __( '', 'posts-character-count-admin' );
}
}
/* Methods and Action for the characters count in the Edit Posts/Pages SubPanel */
public function post_edit_screen_admin_footer() {
global $post;
if ( ! empty( $post ) && isset( $post->post_content ) ) {
$stat = new Posts_Character_Count( $post->post_content );
$template = __(
'Character count:',
'posts-character-count-admin'
) . ' %d ' . __(
'characters (incl. spaces)',
'posts-character-count-admin'
);
printf(
'<script type="text/javascript">
jQuery(document).ready(function ($) {
var $div = $("#post-status-info");
if ($div.length > 0) {
$div.append("<span class=\"inside\" style=\"margin-left: 10px;\">%s</span>");
}
});
</script>', sprintf( $template, $stat->count_characters() )
);
}
}
} // End class
add_filter('manage_edit-post_sortable_columns', 'add_count_sortable_column');
function add_count_sortable_column($sortable_columns){
$sortable_columns['count'] = 'count_count'; //пробовал здесь просто date, опустив след. пункт, все равно сортирует рандомно..
return $sortable_columns;
}
add_filter('pre_get_posts', 'add_column_count_request');
function add_column_count_request( $object ){
if( $object->get('orderby') != 'count_count' )
return;
$object->set('meta_key', '<b>ЧТО ТУТ НУЖНО ПОСТАВИТЬ</b>');
$object->set('orderby', 'meta_value_num');
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question