Loop with ACF relationship field & Orderby "post__in"

I am trying to sort an ACF relationship loop so that the order set in the backend via drag and drop takes effect. However, I can’t find a way to output the posts in descending order. It seems like it works in ascending order by default, but order=desc has no effect on sorting.

In order to solve the sorting issue, i tried to use orderby=post__in, but unfortunately that didn’t work either. I didn’t find anything about this in the documentation or in the forum, but is it possible that orderby=post__in is not supported yet?

I also couldn’t get the ACF relationship field to work with <loop acf_relationship=field_name>, so I solved it like this: <loop type=post,my_cpt id="{field my_acf_relationship_field_name}">. Is there something I missed or what could be the reason that it doesn’t work with acf_relationship=field_name?

First up, I noticed that your tags are written in lower case, which isn’t correct. You mention that you’ve got it working so I assume that’s just some side-effect of your code getting processed by Discourse. Pro-tip: when you’re writing code, be sure to wrap it in backticks, like this ` (or use the code option in the post editor) if you want to write inline code. I’ve modified your post to make your tags visible.

Regarding your issue, I think it might be similar to this thread. Basically, I don’t think that ACF relationship loops accept query parameters like the post loop does. I imagine it might be possible to add this feature, but that’s at least how it works right now. So the approach I suggested in that other thread is the same one you came up with.

I think that’s correct. I’m seeing a feature request here related to this that hadn’t been released yet.

Thanks for the quick reply. Yes, I think the tags were automatically corrected, sorry for that. For better understanding I add all my tested loop tags in correct formatting at the end of this post.

Your solution as described in the other thread doesn’t work in this case, unfortunately, because I don’t have a meta field that I could use for sorting. The sorting should be exclusively from the order in which the posts were set in the ACF-Relationship field, but instead of ASC order in DESC order.

If that is not possible, maybe there is a workaround to reverse the order of the post IDs before I set the final variable for the loop? Possibly using list or format tags? I have not used them a lot, so I am not quite sure if such a task could be achieved with these tags.


Here are the tested loop tags with further comments on each of them

My first approach was to use the acf relationship loop as described in the documentation:

<Loop acf_relationship=my_acf_relationship_field_name>
  <Field title />
</Loop>

This loop did not work at all - no posts were output as if the loop was empty. I had to rewrite it so that I store the post IDs from the relationship field as a variable and then feed those post IDs into a normal loop tag, like this:

<Set name=acf_relationship_post_ids>
  <Field my_acf_relationship_field_name/>
</Set>

<Loop type=my_cpt id="{Get acf_relationship_post_ids}">
  <Field title />
</Loop>

This loop correctly outputs the posts selected in the ACF relationship field. Without defining a parameter for the order within the loop tag, all posts are automatically displayed in ascending order according to their respective order in the ACF relationship field. However, it is not possible to display the posts in descending order.

For example the following loop tag still shows all posts in ascending order:

<Loop type=my_cpt id="{Get acf_relationship_post_ids}" order=desc>

To solve the sorting problem I tried to set orderby=post__in and order=desc, but without success:

<Loop type=my_cpt id="{Get acf_relationship_post_ids}" orderby=post__in order=desc>

Well, that’s not good! I just tested this exact syntax on my end and it works as expected (it displays posts in the order they’re listed in the ACF relationship field). Are you able to test whether there’s a conflict with some third-party plugin or if there’s anything unique about your particular installation that might be causing this? It’s a pretty core feature of L&L and there are lots of other people on the forum using relationship loops, so this seems like there might be something unique going on on your particular site that’s affecting how that field works. Are you able to whip up a fresh WP install and see if you’re able to replicate this issue with the relationship loop not working? Or maybe you could simply try deactivating other plugins to see if it’s a plugin conflict that’s causing this.

Regarding sorting your posts, it seems that there’s no simple way to reverse the order of your posts. To achieve what you’re trying to do, I think the feature request I linked in my earlier post will need to be implemented so that you can write orderby=include order=desc.

There’s undoubtedly a way to reverse the order of a loop within L&L, but I expect that it would require way too much processing to make it happen compared to simply waiting for the feature to be released in the core plugin. That being said, if you’re interested in a potential hacky workaround, something like this might work:

<List name=post_ids json><Field my_relationship_field /></List>

<Set reordered_ids>
  <Loop list=post_ids>
    <Field list=post_ids item="{Math}{Get loop=total /} - {Get loop=count /} + 1{/Math}" /><If not last>,</If>
  </Loop>
</Set>

<Loop type=post id="{Get reordered_ids}">
  <p><Field title /></p>
</Loop>

As you can see, it’s a lot of processing for something simple, but I think it’s possible. WOuldn’t recommend doing it this way though. I can follow up on this thread when that orderby=include gets added but I’m not sure how long that’ll be. I’ll prompt the devs on that to try to get that released.

Thanks for you further assistance, I really appreciate it. I tested the ACF relationship loop a little further and can confirm that it works as intended in “normal use”. I think the reason why the ACF loop doesn’t work for me, is because I use the ACF relationship loop inside a user loop (the ACF relationship field is part of the user profile) like this:

<Loop type=user id=current>
	<Loop acf_relationship=my_acf_relationship_field_name>
		<Field title />
	</Loop>
</Loop>

What’s a little odd is that the post IDs are generally output correctly if I output the ACF relationship field as follows:

<Loop type=user id=current>
	<Field acf_relationship=my_acf_relationship_field_name />
</Loop>

It just doesn’t work as an ACF relationship loop, but I think I’ve already found a solution for all of the issues. There is a filter from ACF that allows you to customize the return value so that you can reverse the the order (see: ACF | acf/load_value). This may be a special case, but if anyone ever needs this, here is how to achieve a reverse order for the relationship field (place this code inside functions.php or any code snippet plugin):

function reverse_acf_relationship_value( $value, $post_id, $field )
{
    if( !is_admin() && is_array( $value ) ) {
		$value = array_reverse( $value );
	}
	
    return $value;
}
add_filter('acf/load_value/name=my_acf_relationship_field_name', 'reverse_acf_relationship_value', 10, 3);

To output an ACF relationship field that was created as a user field in reverse order, a loop like this should work in combination with the filter above:

<Loop type=user id=current>
	<Loop type=my_cpt id="{Field acf_relationship=my_acf_relationship_field_name}">
		<Field title />
	</Loop>
</Loop>
1 Like