# Elementor Posts Query: Related Posts From the Same Category

> Adds a custom query ID "wpide_related" that shows posts sharing a category with the current post, newest first, without the current post itself.

- Type: PHP
- Runs: Run everywhere
- Category: [Page Builders & Themes](https://wpide.com/library/category/page-builders/)
- Tags: elementor, query, related, categories
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/elementor-query-related-by-category/

## Code

```php
add_action( 'elementor/query/wpide_related', function ( $query ) {
    if ( ! is_singular() ) {
        return;
    }

    $post_id = get_queried_object_id();
    $terms   = wp_get_post_terms( $post_id, 'category', array( 'fields' => 'ids' ) );

    if ( is_wp_error( $terms ) || empty( $terms ) ) {
        return;
    }

    $query->set( 'category__in', $terms );
    $query->set( 'post__not_in', array( $post_id ) );
    $query->set( 'ignore_sticky_posts', true );
    $query->set( 'no_found_rows', true );
} );
```

## Notes

Type wpide_related in the Query ID field of a Posts or Loop Grid widget in your single post template.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Elementor Posts Query: Related Posts From the Same Category" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
