Elementor Pro widgets and templates enable us to add posts quickly on WordPress pages. One of the feature is to add posts and filter by category. Although you can add Posts widget on every page and filter category but if you have lot of pages and may want to use one Elementor template on all pages so that you don’t have to update all pages in future one by one for any styling or other issue, better way way is to use template, add the query id custom query filter, and add the container using short code or template or better still even use WordPress hook to include the container on any WordPress page dynamically by calling its shortcode.
In this example, this custom query filter ‘posts_by_category’ will check the current page slug, find its category id if the slug matches with the category slug and filter the posts for your widget added on the Elementor page.
This way, you don’t have to update every page for changes, just create one template of posts, add the query_id in Query section of widget, and add its shortcode on all category pages to filter posts.
Simply add the following custom query filter snippet in your theme functions.php or use Code snippet plugin:
add_action( 'elementor/query/posts_by_category', function( $query ) { global $post; $obj = get_category_by_slug($post->post_name); $cat_id = $obj->term_id; if($cat_id){ $taxonomy = 'category'; $tax_query = array( array( 'taxonomy' => $taxonomy, 'field' => 'id', 'terms' => $cat_id, ), ); $query->set( 'tax_query', $tax_query ); } $query->set('order', 'DESC'); } );
And here is the gist link for this snippet:
Hope that helps.