Elementor Posts Query: Exclude the Current Post
Adds a custom query ID "wpide_exclude_current" for Elementor Pro's Posts and Loop Grid widgets that leaves out the post being viewed, ideal for "More articles" sections.
Opens WPIDE on
Code
add_action( 'elementor/query/wpide_exclude_current', function ( $query ) {
$current = get_queried_object_id();
if ( ! $current || ! is_singular() ) {
return;
}
$exclude = (array) $query->get( 'post__not_in' );
$exclude[] = $current;
$query->set( 'post__not_in', array_map( 'absint', $exclude ) );
} );
Notes
In the widget's Query section, type wpide_exclude_current in the Query ID field.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.