Multiple Post Types in Query Loop

Adding multiple Post Types to Query Loop:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
	if ( 
        ! is_admin() &&
        // if block has an advanced > additional CSS Class of: my-query
        ! empty( $attributes['className'] ) && 
        strpos( $attributes['className'], 'my-query' ) !== false
	) {
        // pass meta_query parameter
        $query_args[ 'post_type' ] = array('post', 'page');
	}
	
	return $query_args;
}, 10, 2 );
4 Likes

This is great, thank you. However, on the query I’m using, the parameter “Exclude current post” doesn’t work anymore. Any way to keep that parameter working while still displaying several post types?

Hi @noringam,

Can you reach out to us here for GB Pro feature inquiries?: Support - GenerateBlocks

Sure thing, thank you

I hope it’s okay to share here; I wanted to offer the community this modified snippet if you want to exclude the current post from the query loop results (useful for “Related Content” type display):

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
	if (
        ! is_admin() &&
        // if block has an advanced > additional CSS Class of: my-query
        ! empty( $attributes['className'] ) &&
        strpos( $attributes['className'], 'my-query' ) !== false
	) {
        // pass meta_query parameter
        $query_args[ 'post_type' ] = array('post', 'page');
        $query_args['post__not_in'] = [get_the_ID()];  // Removes current post from the results
	}

	return $query_args;
}, 10, 2 );