DPS Exclude Displayed Posts

If you’re using the Display Posts Shortcode multiple times in one page, or using it on a page with another loop of posts, use this to exclude already displayed posts from new shortcode queries.

Note: you’ll need to modify non-Display Posts Shortcode queries to add those post IDs to the global variable. This snippet is using a global variable found in the Genesis theme that already stores the list of displayed posts.

More information

/**
 * Exclude displayed posts from DPS query 
 *
 */
function be_dps_exclude_displayed_posts( $args ) {
  global $_genesis_displayed_ids;
  $args['post__not_in'] = !empty( $args['post__not_in'] ) ? array_merge( $args['post__not_in'], $_genesis_displayed_ids ) : $_genesis_displayed_ids;
  return $args;
}
add_filter( 'display_posts_shortcode_args', 'be_dps_exclude_displayed_posts' );
/**
 * Add DPS posts to exclusion list 
 *
 */
function be_dps_add_posts_to_exclusion_list( $output ) {
  global $_genesis_displayed_ids;
  $_genesis_displayed_ids[] = get_the_ID();
  return $output;
}
add_filter( 'display_posts_shortcode_output', 'be_dps_add_posts_to_exclusion_list' );

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk