我正在循环检查我的帖子是否有内容。目前,我在循环中添加了一个条件:
if ( $post->post_content )
并将参数放入
wp query ('posts_per_page' => 8).
我认为它有效,但实际上,WP 查询
去查找最后的 8 个帖子
,并检查这 8 个帖子
的内容。因此它呈现 2
或 3
帖子。
我想要的是一种显示最近 8
有内容的帖子的方法。
明白我的意思了吗?
非常感谢您的帮助:)
最好的问候。
最佳答案
这对于标准的 WP 查询是不可能的,您必须在调用 WP_Query 之前利用 posts_where
的使用。
function filter_where($where = ''){
return $where .= "AND trim(coalesce(post_content, '')) <>''";
}
在上面,我们只选择 post_content
列不为空的帖子。
然后添加过滤器。
add_filter('posts_where', 'filter_where');
现在执行查询。
$query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 8));
然后当你完成后,从查询中删除过滤器,这样它就不会干扰。
remove_filter('posts_where', 'filter_where');
关于php - WP-查询 : check if the post content is empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16395115/