Title: Nested loops
Last modified: January 21, 2021

---

# Nested loops

 *  Resolved [designforhumansstudio](https://wordpress.org/support/users/designforhumansstudio/)
 * (@designforhumansstudio)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/nested-loops-3/)
 * Hi,
    First of all, I also find this Plugin great! I was looking for a solution
   for a publication list since quite a while and this plugin is exactly what I 
   need. Unfortunately I am not able to create a nested loop, which goes through
   all publications and sorts them first under the headline of their publication_types
   and then following the publication_date. Here is my code:
 * [loop type=publications orderby=publication_type]
    <h2 class=”publist”>[field
   publication_type]</h2> [the-loop publication_type=this orderby=field_num key=
   publication_date order=DESC] <p class=”publist”>[field image size=Medium size][
   field authors] ([field acf_date=publication_date date_format=”Y”]): [field title-
   link]. [field publisher].</p> [/the-loop] [/loop]
 * The result is that the publications are shown three times and always under the
   publication_type of each publication. Only the sorting by date I could already
   sort out.
 * I would be super thankful if you can give me a quick hint or a link to a documentation
   for nested loops.
 * Best,
 * Jens
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fnested-loops-3%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [polarracing](https://wordpress.org/support/users/polarracing/)
 * (@polarracing)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/nested-loops-3/#post-13939157)
 * Ok, first of all this `[loop type=...] [the-loop]` is not a nested loop.
 * a nested loop looks like this: `[loop type=...] [-loop type=...]`
 * Try this:
 *     ```
       [loop type=publications orderby=publication_type]
       [pass field=publication_type]
       [-loop type=publications field=publication_type value='{FIELD}' oderby=... WHAT YOU LIKE]
       Your content here
       [/-loop]
       [/pass]
       [/loop]
       ```
   
    -  This reply was modified 5 years, 2 months ago by [polarracing](https://wordpress.org/support/users/polarracing/).
 *  Thread Starter [designforhumansstudio](https://wordpress.org/support/users/designforhumansstudio/)
 * (@designforhumansstudio)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/nested-loops-3/#post-13939417)
 * Thanks a lot! This looks already much better:
    [https://julianculp.academicwebs.com/publications/](https://julianculp.academicwebs.com/publications/)
 * I now only need to solve the problem that the first loop goes through all publications
   and not only through the 2 different publication_type(s). How could I show each
   publication type only once?
 * Here is my new shortcode:
 * [loop type=publications orderby=publication_type]
    [pass field=publication_type]
   <h2 class=”publist”>[field publication_type]</h2> [-loop type=publications field
   =publication_type value='{FIELD}' orderby=field_num key=publication_date order
   =DESC] <p class=”publist”>[field image size=Medium size][field authors] ([field
   acf_date=publication_date date_format=”Y”]): [field title-link]. [field publisher].
   </p> [/-loop] [/pass] [/loop]
 *  [polarracing](https://wordpress.org/support/users/polarracing/)
 * (@polarracing)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/nested-loops-3/#post-13942270)
 * Then you need to limit the pass to only one time.
    Try this:
 *     ```
       [loop type=publications orderby=publication_type]
   
       [pass field=publication_type]
       [if var=var not value={FIELD} and var_2=var]
       [-loop type=publications field=publication_type value='{FIELD}' oderby=... WHAT YOU LIKE]
       Your content here
       [/-loop]
       [/if]
       [/pass]
       [set var][field=publication_type][/set]
       [/loop]
       ```
   
    -  This reply was modified 5 years, 2 months ago by [polarracing](https://wordpress.org/support/users/polarracing/).
 *  Thread Starter [designforhumansstudio](https://wordpress.org/support/users/designforhumansstudio/)
 * (@designforhumansstudio)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/nested-loops-3/#post-13942795)
 * Thank you so much! This last idea helped me to create the final shortcode:
 * [set last][/set]
    [loop type=publications orderby=publication_type] [pass field
   =publication_type] [if var=last not value='{FIELD}’] <h2 class=”publist”>[field
   publication_type]</h2> [-loop type=publications field=publication_type value='{
   FIELD}’ orderby=field_num key=publication_date order=DESC] <p class=”publist”
   >[field image size=Medium size][field authors] ([field acf_date=publication_date
   date_format=”Y”]): [field title-link]. [field publisher].</p> [set last][field
   publication_type][/set] [/-loop] [/if] [/pass] [/loop]
 * It works as it should. I am super happy about this solution!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Nested loops’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-content-shortcode_766976.svg)
 * [Custom Content Shortcode](https://wordpress.org/plugins/custom-content-shortcode/)
 * [Support Threads](https://wordpress.org/support/plugin/custom-content-shortcode/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-content-shortcode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-content-shortcode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-content-shortcode/reviews/)

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [nested](https://wordpress.org/support/topic-tag/nested/)

 * 4 replies
 * 2 participants
 * Last reply from: [designforhumansstudio](https://wordpress.org/support/users/designforhumansstudio/)
 * Last activity: [5 years, 2 months ago](https://wordpress.org/support/topic/nested-loops-3/#post-13942795)
 * Status: resolved