• Resolved zeaks

    (@zeaks)


    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 https://pastebin.com/kf5v7NQf
    Here is the loop https://pastebin.com/rYk8JdJT

Viewing 3 replies - 1 through 3 (of 3 total)
  • your code looks compliant with the code in this post https://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?

    Thread Starter zeaks

    (@zeaks)

    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(); ?>
    Thread Starter zeaks

    (@zeaks)

    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Exclude post formats from custom loop’ is closed to new replies.