I’m trying to use get_posts to only return posts that have an excerpt. Done lots of searching and have been trying to use “posts_where” filter on the query but my SQL is lacking. This is what I’m using which in theory I think should work, but really I have no idea on the sql and can’t work out how to print the sql string for this query to debug…
$args = array(
'post_type' => 'testimonial',
'numberposts' => 1,
'orderby' => 'rand',
);
add_filter( 'posts_where' , 'posts_where_excerpt_not_empty' );
$post = get_posts($args);
remove_filter( 'posts_where' , 'posts_where_excerpt_not_empty' );
[...]
function posts_where_excerpt_not_empty( $where ) {
$where .= " post_excerpt NOT NULL";
return $where;
}
The
post_excerpt
column is a string and is not filterable using “IS NOT NULL” . To query for an empty string you can use the!=
operator.Also get_posts suppresses filters by default so you will need to call it with suppress filters set to false or use another query method.
i Might be mistaken but wont a “check if empty” condition do the trick?
.
I think this should be inside the loop in the page you want to display the content retured from the check..