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;

2 Answers 2


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.

function posts_where_excerpt_not_empty( $where ) {
    $where .= " AND post_excerpt != '' ";
    return $where;

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.

$posts = get_posts( array( 'suppress_filters' => FALSE ) );

i Might be mistaken but wont a "check if empty" condition do the trick?

'post_type' => 'testimonial',
'showposts' => 1,
'orderby' => 'rand'
) );  

$checExcerpt = get_the_excerpt();
if !($checExcerpt = '') {
// Put the echo commands here
} else {
echo 'Sorry You forgot to enter Descriptions.. search engine love does!';
} ?>

I think this should be inside the loop in the page you want to display the content retured from the check..

  • Yeah that will work but only after the post has been retrieved. I'm trying to only hit the database once and so need to add it into the query..
    – patnz
    Nov 7, 2011 at 19:58
  • You mean while the post has being retreived.. Anyhow maybe i am missing some information.. Meanin the "what for".. dont you want to print out results that got excerpt? like a category page with results that got excerpt?
    – Sagive
    Nov 8, 2011 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.