add_action('elementor/query/related_bikes_by_slug', function($query) {

    if (!($query instanceof \WP_Query)) {
        return;
    }

    // Anti-recursion: als deze query binnen dezelfde request opnieuw wordt aangeroepen, stop
    static $running = false;
    if ($running) {
        $query->set('post__in', [0]);
        $query->set('posts_per_page', 0);
        return;
    }

    $running = true;

    try {
        $context_id = (int) get_queried_object_id();
        if (!$context_id) {
            $query->set('post__in', [0]);
            $query->set('posts_per_page', 0);
            $running = false;
            return;
        }

        // Start altijd met de huidige pagina/post
        $ids = [$context_id];

        $raw = '';
        if (function_exists('get_field')) {
            $raw = get_field('related_bike_slugs', $context_id);
        }

        if (is_string($raw) && trim($raw) !== '') {

            // ondersteunt: "a,b" én regels onder elkaar
            $slugs = preg_split('/[,\r\n]+/', $raw, -1, PREG_SPLIT_NO_EMPTY);
            $slugs = array_values(array_filter(array_map('trim', $slugs)));

            foreach ($slugs as $slug) {
                $slug = sanitize_title($slug);

                // Zoek in pages én posts
                $p = get_page_by_path($slug, OBJECT, ['page', 'post']);

                if (!$p) {
                    $q = new \WP_Query([
                        'post_type'      => ['page', 'post'],
                        'name'           => $slug,
                        'post_status'    => 'publish',
                        'posts_per_page' => 1,
                        'no_found_rows'  => true,
                        'fields'         => 'ids',
                        'suppress_filters' => true,
                    ]);
                    if (!empty($q->posts[0])) {
                        $p = get_post((int) $q->posts[0]);
                    }
                }

                if ($p && !empty($p->ID)) {
                    $candidate_id = (int) $p->ID;
                    if (!in_array($candidate_id, $ids, true)) {
                        $ids[] = $candidate_id;
                    }
                }

                // max 3 totaal: huidig + 2
                if (count($ids) >= 3) {
                    break;
                }
            }
        }

        $query->set('post_type', ['page', 'post']);
        $query->set('post_status', 'publish');
        $query->set('post__in', $ids);
        $query->set('orderby', 'post__in'); // volgorde: huidig, slug1, slug2
        $query->set('posts_per_page', count($ids));
        $query->set('no_found_rows', true);

    } catch (\Throwable $e) {
        $query->set('post__in', [0]);
        $query->set('posts_per_page', 0);
        // if (defined('WP_DEBUG') && WP_DEBUG) { error_log($e->getMessage()); }
    }

    $running = false;
});
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//elektrischefietser.nl/main-sitemap.xsl"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<sitemap>
		<loc>https://elektrischefietser.nl/page-sitemap.xml</loc>
		<lastmod>2026-01-29T17:12:02+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://elektrischefietser.nl/category-sitemap.xml</loc>
		<lastmod>2026-01-29T17:12:02+00:00</lastmod>
	</sitemap>
</sitemapindex>
<!-- XML Sitemap generated by Rank Math SEO Plugin (c) Rank Math - rankmath.com -->