Support » Fixing WordPress » Change loop to display specific format

  • I currently have the following php code for the content loop on my homepage.

    <?php
    			if ( have_posts() ) :
    				// Start the Loop.
    				while ( have_posts() ) : the_post();
    
    					get_template_part( 'content', get_post_format()  );
    
    				endwhile;
    
    				twentyfourteen_paging_nav();
    
    			else :
    
    				get_template_part( 'content', 'none' );
    
    			endif;
    		?>

    I want to know how I can specifically select one post format. I want the loop to only display posts of the standard post format.

Viewing 1 replies (of 1 total)
  • You need to select the IDs of all posts that have a format and re-issue the query while excluding them. Try inserting this just ahead of the if ( have_posts() ) : line:

    $sql = "SELECT tr.object_id
       FROM $wpdb->term_relationships tr
       JOIN $wpdb->term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
       WHERE tt.taxonomy = 'post_format'
    ";
    $posts_with_format = $wpdb->get_col($sql);
    
    query_posts(
       array_merge(
          $wp_query->query,
          array('post__not_in' => $posts_with_format)
       )
    );

    This could cause a problem if you have a large number of posts with formats. In that case, you will have to resort to a more complex method.

Viewing 1 replies (of 1 total)
  • The topic ‘Change loop to display specific format’ is closed to new replies.