按大小排序可以添加自定义列并按post\\u内容长度排序。但恐怕效率不高。
首先,您必须添加自定义列并在其中显示内容的长度。我假设您对经过修剪的内容感兴趣(因此“”仍然为空)。
然后,您必须使此列可排序,并编写自定义代码来对帖子进行排序。
/* Add custom column to post list */
function add_content_size_column( $columns ) {
return array_merge( $columns, array( \'content_size\' => __( \'Size\', \'your_text_domain\' ) ) );
}
add_filter( \'manage_posts_columns\' , \'add_content_size_column\' );
/* Display custom column */
function display_posts_content_size( $column, $post_id ) {
if ( \'content_size\' == $column ) {
echo mb_strlen( trim( get_post_field( \'post_content\', $post_id ) ) );
}
}
add_action( \'manage_posts_custom_column\' , \'display_posts_content_size\', 10, 2 );
/* Set custom column as sortable */
function set_content_size_column_as_sortable( $columns ) {
$columns[\'content_size\'] = \'content_size\';
return $columns;
}
add_filter( \'manage_edit-post_sortable_columns\', \'set_content_size_column_as_sortable\' );
function my_content_size_orderby( $orderby ) {
global $wpdb;
// Run this filter only once, when needed (it will be added by pre_get_posts)
remove_filter( \'posts_orderby\', \'my_content_size_orderby\' );
$orderby = str_replace( "{$wpdb->posts}.post_date ", "CHAR_LENGTH( TRIM({$wpdb->posts}.post_content) ) ", $orderby );
return $orderby;
}
function my_content_size_modify_orderby( $query ) {
if ( ! is_admin() ) return;
if ( \'content_size\' == $query->get( \'orderby\' ) ) {
add_filter( \'posts_orderby\', \'my_content_size_orderby\' );
}
}
add_action( \'pre_get_posts\', \'my_content_size_modify_orderby\' );
有一种非常简单的方法可以防止帖子被发布。你所要做的就是自己写
save_post
行动您可以检查帖子是否为空,如果为空,您可以将其状态更改为
draft
(或
pending
, 或者你想要的任何东西)。
你需要记住的唯一一件事是,WP会显示一些消息,说明帖子已经发布。您可以使用更改该消息redirect_post_location
滤器
function my_save_post( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
// Now perform checks to validate your data.
$prevent_publish = (\'\' == trim( $_POST[\'content\'] )); // Set to true if data was invalid.
if ( $prevent_publish ) {
// Unhook this function to prevent indefinite loop
remove_action( \'save_post\', \'my_save_post\' );
// Update the post to change post status
wp_update_post( array(
\'ID\' => $post_id,
\'post_status\' => \'draft\'
) );
// Re-hook this function again
add_action( \'save_post\', \'my_save_post\' );
}
}
add_action( \'save_post\', \'my_save_post\' );
function my_redirect_location( $location, $post_id ) {
if ( isset($_POST[\'publish\']) ) {
// Obtain current post status
$status = get_post_status( $post_id );
// The post was \'published\', but if it is still a draft, display draft message (10).
if ( \'draft\' === $status ) {
$location = add_query_arg( \'message\', 10, $location );
}
}
return $location;
}
add_filter( \'redirect_post_location\', \'my_redirect_location\', 10, 2 );