Skip to content

Instantly share code, notes, and snippets.

@LeMiira
Created August 7, 2025 14:22
Show Gist options
  • Select an option

  • Save LeMiira/7ef858de7f49939033c19f34e25cf155 to your computer and use it in GitHub Desktop.

Select an option

Save LeMiira/7ef858de7f49939033c19f34e25cf155 to your computer and use it in GitHub Desktop.
RelatedPosts
add_action('elementor/query/relatedPostsBlog', function ($query) {
$page_id = get_the_ID();
// Get the terms in the 'page-categ' taxonomy for the current page
$terms = get_the_terms($page_id, 'page-categ');
if (!empty($terms) && !is_wp_error($terms)) {
// Use the first term (or adapt to handle multiple terms if needed)
$term_slug = $terms[0]->slug;
// Query posts with tag matching the slug
$query->set('post_type', 'post');
$query->set('posts_per_page', 3);
$query->set('orderby', 'date');
$query->set('order', 'DESC');
$query->set('tag', $term_slug); // Matches by tag SLUG
$query->set('post_status', 'publish');
$query->set('ignore_sticky_posts', true);
} else {
// If no matching term, return nothing
$query->set('post__in', [0]);
}
// Prevent the hook from firing again accidentally
remove_action('elementor/query/relatedPostsBlog', __FUNCTION__);
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment