• Hi ??

    Using the awesome relationsship features of pods, I’m listing related materials in this template snippet:

    <ul>
    [each ma-pi]
    	<li><a href="/{@ma-pi.post_name}">{@ma-pi.post_title}</a></li>
    [/each]
    </ul>
    

    The output is as on the linked page. Is it possible to apple a “order by” to the [each] command?

    I tried editing the Additional Field Options of the pod itself, there is a field called “Customized ORDER BY”. Tried with “title”, “post_title” and “post_title ASC” – no change.

    Any ideas?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Jim True

    (@jimtrue)

    You can’t do this within the [each][/each] block because they weren’t intended to replace PHP.

    However, you could create another template that output the listed items and call it with a shortcode within your template:

    [pods name="books" where="related_author.ID = {@ID}" template="Book List" orderby="post_title"]

    I’m using the related_field in the other post-type to pull the list of items from. If this is confusing, you can jump on our Slack Chat at https://pods.io/chat/ and I’ll explain.

    You have an error in your [each] example above; when you loop through an [each related_field][/each] within the loop, you don’t repeat related_field. You should only reference the field you’re attempting to display. By repeating it, you reach out of the loop.

    
    <ul>
    [each ma-pi]
    	<li><a href="/{@permalink,esc_url}">{@post_title}</a></li>
    [/each]
    </ul>
    

    I also changed your href to permalink; not sure how post_name would’ve worked for you.

    Thread Starter Bjarne Oldrup

    (@oldrup)

    @jim True, you’re the best! Thank you so much. With my pods, the resulting code ended up being:

    <ul>
    [pods name="materiale" orderby="title  where="ma-pi.ID={@ID}" template="li-materiale"]
    </ul>

    As I already had the li-materiale template in place (and thanks for drawing attention to the permalink error), that was all I needed. Output is now sorted alphabetically. Performance is ok, page loads in 1.2 seconds, and TTFB is not great on my host at the moment anyway.

    Thank you once again – love the possibilities with pods ??

    • This reply was modified 6 years, 9 months ago by Bjarne Oldrup.
    Plugin Contributor Jim True

    (@jimtrue)

    Glad to hear it ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using [each], how to sort/order by?’ is closed to new replies.