正如Tom在评论中所说,没有选择WP_Query
按字数/长度排序帖子。
如果您想这样做,那么您必须首先将每个帖子的字数或长度存储为post meta。一旦存储好,您就可以使用WP_Query
像这样:
$args = array(
\'posts_per_page\' => 10,
\'post_status\' => \'publish\',
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'wordcount\',
\'order\' => \'DESC\',
);
$query = new WP_Query( $args );
更新帖子或使用以下代码创建新帖子时,可以将帖子的字数存储为帖子元:
/**
* Get post wordcount and save as post meta
*/
function add_post_wordcount( $post_id ) {
$content = get_post_field( \'post_content\', $post_id ); // Get the content
$wordcount = str_word_count( strip_tags( $content ) ); // Count the words
if ( ! add_post_meta( $post_id, \'wordcount\', $wordcount, true ) ) {
update_post_meta( $post_id, \'wordcount\', $wordcount );
}
}
然后,对以下WordPress操作运行此函数:
On save new post
add_action( \'save_post\', \'add_post_wordcount\' );
On updating existing post
add_action( \'post_updated\', \'add_post_wordcount\' );