This is only a partial answer, providing a couple of functions that can be used to get the text under a headline, given its custom ID. It does not attempt to incorporate anything into a snippet (which, as I explained in a comment, I don't think is a good idea ). It does provide an interactive command, bound to a key for convenient access.
Here is an Org file that can be used to define and test the functions:
* Link https://emacs.stackexchange.com/questions/81758/use-contents-of-a-heading-in-yasnippet * Heading 1 :PROPERTIES: :CUSTOM_ID: MyHead :END: Text under Heading 1 * Code :noexport: #+begin_src elisp :results drawer (defun my/org-get-text-of-headline (beg end) (let ((s (buffer-substring beg end))) (with-temp-buffer (insert s) (org-mode) ;; skip the headline and property drawer and ;; don't include trailing newlines. (let* ((e (org-element-parse-buffer)) (parlist (org-element-map e '(paragraph) (lambda (x) (org-element-property :begin x)))) (end (org-element-property :end e))) (string-trim (buffer-substring (car parlist) end)))))) (defun my/org-get-text-under-given-heading (id) (save-excursion (org-link-search (format "#%s" id)) (let* ((e (org-element-at-point)) (beg (org-element-property :begin e)) (end (org-element-property :end e))) (my/org-get-text-of-headline beg end)))) #+end_src #+RESULTS: :results: my/org-get-text-under-given-heading :end: #+begin_src elisp :results drawer (my/org-get-text-under-given-heading "MyHead") #+end_src #+RESULTS: :results: Text under Heading 1 :end:
C-c C-c on the first source block to define the functions and then C-c C-c on the second source block to test.
The second function finds the headline with the given Custom ID, parses the headline to find out where it begins and where it ends and then passes that region to the first function.
The first function creates a temporary buffer and copies the region into it, sets up the major mode to be org-mode and then parses the buffer (that's why I copied the headline into a temp buffer: parsing an Org mode file is a bit slow, so if the original file had tens or hundreds of headings, I didn't want to have to parse all of them - I just need one).
The function then uses the resulting parse tree to skip the initial stuff by selecting just the paragraphs. In fact, we only need the beginning of the first paragraph: we get the region between that and the end of the node and that's the wanted text.
There is no error checking, and there is an assumption that the relevant node is simple, i.e. it does not have any subtrees (you might want to try to see what happens if that assumption is not valid).
The functions can be used as a building block for other things, e.g. you can write a command that calls my/org-get-text-under-given-heading and stuffs the resulting text into the kill ring - you can then insert it wherever you want by yanking with C-y. And you can bind it to a key for easy access:
#+begin_src elisp :results drawer (defun my/text-under-heading-as-kill (&optional custom-id) (interactive "sCustom ID: ") (kill-new (my/org-get-text-under-given-heading custom-id))) (define-key global-map (kbd "C-c z") #'my/text-under-heading-as-kill) #+end_src
You type C-c z, enter the custom ID at the prompt and you now have the string in your kill ring. Move to wherever you want to insert it and yank with C-y.