如何根据帖子内容的长度排序帖子查询?
$orderby=?;
$query = array(
\'posts_per_page\' => 10,
\'post_status\' => \'publish\',
\'orderby\' => $orderby,
\'order\' => \'DESC\',
);
$the_query = new WP_Query($query);
如何根据帖子内容的长度排序帖子查询?
$orderby=?;
$query = array(
\'posts_per_page\' => 10,
\'post_status\' => \'publish\',
\'orderby\' => $orderby,
\'order\' => \'DESC\',
);
$the_query = new WP_Query($query);
正如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\' );
嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post