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.

How do I prevent posts with no title showing up in my query / loop ? ( I am doing aside, quote etc post-formats and I want to exclude title less posts from one particular loop)

I have nothing on this. I searched through WP_Query on this but I can't find anything, and when I try google everything is muddied with 'not display title stuff' etc. Is it possible?

share|improve this question
    
Take a look at wordpress.stackexchange.com/questions/11822/… , maybe you can do a query –  chifliiiii Aug 16 '12 at 23:17
    
How complex is your query? Can you post a sample here? –  Brian Fegter Aug 16 '12 at 23:28
add comment

1 Answer

up vote 2 down vote accepted

Please find below a quick solution to your problem

  1. first query for all empty titles
  2. place the result in an array
  3. query again for post__not_in see below sample code

    $query = new WP_Query('post_title=\'\'');
    $a_empty_titles = array();
     while($query->have_posts()){
         $query->the_post();
         array_push( $a_empty_titles ,$query->post->ID);
     }
     wp_reset_postdata();
     wp_reset_query();
     echo count($a_empty_titles);
     $query = new WP_Query( array( 'post__not_in' => $a_empty_titles ) );
     //your loop code here
     //reset query
     wp_reset_postdata();
     wp_reset_query();
     ?>
    
share|improve this answer
    
nice code, thanks –  byronyasgur Aug 17 '12 at 11:56
    
actually i cant get it to work - $query = new WP_Query('post_title=\'\''); seems to not be exactly right, it picks up on more than just posts with no title for some reason. I cant figure out what causes it to pick certain posts that do have titles. Any ideas. I will work on it again later. –  byronyasgur Aug 17 '12 at 13:52
    
try this code: global $wpdb; $not_empty_titles = $wpdb->get_results( " SELECT ID FROM $wpdb->posts WHERE post_title != '' ",ARRAY_N ); print_r($not_empty_titles); –  xiarnousx Aug 17 '12 at 21:34
    
Class Reference WPDB for reference –  xiarnousx Aug 17 '12 at 21:35
    
Thanks. I'm sure that would work, though I was trying to avoid querying directly ... based on your original idea I was able to get it working something like this ... thanks .... $a_empty_titles = array(); $args = array( 'post_status' => 'publish' ); $fp_posts = get_posts( $args ); foreach( $fp_posts as $post ) if ( ! $post->post_title ) array_push($a_empty_titles,$post->ID); $args = array( 'post__not_in' => $a_empty_titles ); –  byronyasgur Aug 18 '12 at 11:11
add comment

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.