MailFix
@MailFix
Хороший человек

Как произвести сортировку по произвольному полю WordPress для количество символов?

Использую плагин на WP для подсчета и вывода количество символов постов https://wordpress.org/plugins/posts-character-coun...

Вот php файл
<?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');
}


ЧТО ТУТ НУЖНО ПОСТАВИТЬ - что нужно тут поставить?
Я удалил текст в плагине, чтобы он просто выводил цифры, поэтому можно использовать meta_value_num
И если все таки использовать не только цифровые поля, тогда вместо meta_value_num использовать meta_value?
  • Вопрос задан
  • 369 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы