WP-Query:检查帖子内容是否为空


WP-Query : check if the post content is empty

我想检查我的帖子是否有内容,在循环中。目前,我在循环中添加了一个条件:

if ( $post->post_content ) 

和放在

中的参数
wp query ('posts_per_page' => 8).

我认为它工作,但实际上,WP query去找到最后一个8 posts,并检查那些8 lasts的内容。所以它呈现23的帖子。

我想要的是一种方式来显示最近的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');