WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude post formats from custom loop (4 posts)

  1. zeaks
    Member
    Posted 1 year ago #

    I need to exclude all post formats but the standard from a page template loop that displays 4 most recent posts. This is the code I've been trying to get working, after 3 hours I can make it do just about everything except what I want.

    $args = array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'post_format',
    			'field' => 'slug',
    			'terms' => array( 'post-format-aside', 'post-format-image' ),
    			'operator' => 'NOT IN'
    		)
    	)
    );
    $query = new WP_Query( $args );

    Here is the page template http://pastebin.com/kf5v7NQf
    Here is the loop http://pastebin.com/rYk8JdJT

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    your code looks compliant with the code in this post http://wordpress.mfields.org/2011/post-format-queries/

    how does the WP_Query() code fit into the code of the page template?

    btw: the exclusion seems to work in a page template in my local test setup runing Twenty Twelve; my example code:

    <?php $formats = new WP_Query( array(
    	'posts_per_page' => 20,
    	'paged' => get_query_var('paged'),
    	'tax_query' => array(
    		array(
    		'taxonomy' => 'post_format',
    		'field'    => 'slug',
    		'terms'    => array( 'post-format-link', 'post-format-aside', 'post-format-gallery' ),
    		'operator' => 'NOT IN',
    		)
    	 )
    ));
    if( $formats->have_posts() ) : while( $formats->have_posts() ) : $formats->the_post();
    echo '<h4>'; the_title(); echo '</h4>';
    endwhile; endif; wp_reset_postdata(); ?>

    could there be a possible interference from a plugin?

  3. zeaks
    Member
    Posted 1 year ago #

    I don't have any plugins installed but wp-pagenavi which I've removed, and it's a new wp install.

    I tried this using your example as a test template and it limits the posts but won't filter the formats. The first two posts it displays are an aside and gallery format.

    <?php get_header(); ?>
    <?php $formats = new WP_Query( array(
    	'posts_per_page' => 4,
    	'paged' => get_query_var('paged'),
    	'tax_query' => array(
    		array(
    		'taxonomy' => 'post_format',
    		'field'    => 'slug',
    		'terms'    => array( 'post-format-link', 'post-format-aside', 'post-format-gallery' ),
    		'operator' => 'NOT IN',
    		)
    	 )
    ));
    if( $formats->have_posts() ) : while( $formats->have_posts() ) : $formats->the_post();
    	echo '<h4>'; the_title(); echo '</h4>';
    		endwhile; endif; wp_reset_postdata(); ?>
    
    <?php get_sidebar( 'front' ); ?>
    <?php get_footer(); ?>
  4. zeaks
    Member
    Posted 1 year ago #

    Wow nevermind, I think I've been staring at this for too long, it does work.
    I had been playing around with the posts formats and had them all set as links and quotes, everything but what I had excluded.

    Thanks for the help

Topic Closed

This topic has been closed to new replies.

About this Topic