Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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 ) );
share|improve this answer

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

<?php 
query_posts(array( 
'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..

share|improve this answer
    
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 '11 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 SEO Nov 8 '11 at 5:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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