I’m working on a WordPress website using Elementor Pro and Advanced Custom Fields (ACF) for a sports club that offers multiple sports activities. I’ve created a template that reflects the structure of an individual sports offer page – for example, the title of the sport, a description, and an image. So far, so good, and this part is working as expected.
However, I also want to display news/blog posts related to each specific sport offer on its respective page. For instance, when I am on the Badminton page, I want to show blog posts related to Badminton. When I am on the Basketball page, I want to show blog posts related to Basketball.
Here’s what I’ve done so far:
-
Defined blog categories as taxonomies in ACF. (Field types for spot offer)Basketball) as a category.
-
Linked these categories to the respective sport offer pages.
-
Assigned the corresponding categories to the blog posts. The problem is that when I try to display the blog posts, all posts are shown, regardless of the category. I can’t seem to make the posts dynamically reflect the current sport offer. ACF dynamic key
-
edited function.php, but it didn’t work (here my taxonomy was named cat, I changed it to category now) :
// functions.php
function filter_posts_by_custom_field($query) {
if ($query->is_main_query() && !is_admin() && is_singular('sportabteilungen')) {
$term_id = get_field('blog_kategorie');
if ($term_id) {
$query->set('cat', $term_id);
}
}
}
add_action('pre_get_posts', 'filter_posts_by_custom_field');
Do I even need to edit the function.php? What am I missing or doing wrong? How can I ensure that only the relevant blog posts for each sport offer are displayed?
Any guidance or solutions would be greatly appreciated!