diff --git a/scripts/docsutils/get-bbmapping b/scripts/docsutils/get-bbmapping
new file mode 100755
index 0000000..3b78c0e
--- /dev/null
+++ b/scripts/docsutils/get-bbmapping
@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+#
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Simple utility script called from run-docs-build, to be able to get the
+# mapping between a Yocto release name and a BitBake branch, by parsing the
+# releases.json file from https://dashboard.yoctoproject.org/releases.json.
+#
+# Takes the releases.json file as first argument and branch name as second.
+# Prints the mapping when found, or empty string in all other cases.
+
+import json
+import sys
+
+releases_json_path = sys.argv[1]
+branch_name = sys.argv[2]
+
+with open(sys.argv[1], "r") as fd:
+    releases = json.load(fd)
+
+for release in releases:
+    if branch_name.capitalize() == release.get("release_codename", ""):
+        print(release.get("bitbake_version", "").strip())
+        exit(0)
diff --git a/scripts/run-docs-build b/scripts/run-docs-build
index 57044f2..157a201 100755
--- a/scripts/run-docs-build
+++ b/scripts/run-docs-build
@@ -189,6 +189,38 @@ git clean -ffdx
 
 export LATEXMKOPTS="-silent"
 
+function checkout_closest_ref() {
+  # The purpose of this function is to take a ref from yocto-docs (ref=$1) and
+  # checkout the closes possible ref on given (repo=$2).
+  # The way it works is by:
+  # 1. Trying to checkout the same ref on $repo.
+  # 2. If this fails, look for the tag closest to $ref on yocto-docs (CWD) and
+  #    checkout this tag on $repo.
+  # If neither work, exit.
+  local repo="$1"
+  local ref="$2"
+  local bbmapping="$3"
+
+  # keep a copy of the original ref for later
+  docsref="$ref"
+  if [ -n "$bbmapping" ]; then
+    ref="$bbmapping"
+  fi
+
+  if git -C "$repo" rev-parse --verify --quiet "origin/${ref}" >/dev/null; then
+    echo "Closest ref from $docsref for $repo: origin/$ref"
+    git -C "$repo" checkout "origin/$ref"
+    return
+  fi
+  ydocs_closest_tag=$(git describe --tags --match "yocto-*" --abbrev=0 "$docsref")
+  if ! git -C "$repo" rev-parse --verify --quiet "${ydocs_closest_tag}"; then
+    echo "Tag $ydocs_closest_tag does not exists in $repo, quitting"
+    exit 1
+  fi
+  echo "Closest ref from $docsref for $repo: $ydocs_closest_tag"
+  git -C "$repo" checkout "$ydocs_closest_tag"
+}
+
 for branch in "$ypdocsbranch" dunfell $(git branch --remote --contains "$first_sphinx_commit" --format '%(refname:lstrip=3)') $(git tag --contains "$first_sphinx_commit" --contains "$first_dunfell_sphinx_commit" 'yocto-*'); do
     if [ "$branch" = "HEAD" ]; then
         continue
@@ -215,6 +247,14 @@ for branch in "$ypdocsbranch" dunfell $(git branch --remote --contains "$first_s
     git reset --hard
     git clean -ffdx
 
+    # Tags/branches before this commit in yocto-docs do not have automatic library
+    # functions documentation for openembedded-core.
+    if git merge-base --is-ancestor 2c5846cc2c2649b29de7e7a6ac07fef5c2e7f302 HEAD; then
+      bbmapping="$("$scriptdir"/docsutils/get-bbmapping "$builddir/releases.json" "$branch")"
+      checkout_closest_ref "$bbdocs" "$branch" "$bbmapping"
+      checkout_closest_ref "$oecore" "$branch" ""
+    fi
+
     if [ -e "${scriptdir}/docs-build-patches/${branch}/" ]; then
         echo "Adding patch for $branch"
         git apply -3 "${scriptdir}/docs-build-patches/${branch}/"000*
