Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
thomas.diesenreiter

Custom field sorting by number of fields doesnt work

We have an acf post relation ship field with which we link different cpt with each other. We want to show in the post table the number of entries of each post and to be able to sort by it. The sum function works without problem with the setting of custom field / number of fields, but sorting doesn’t at all. The result looks pretty random. It looks like it sorts be the value of the first entry, not by the sum.

3 years, 8 months ago
Stefan van den Dungen Gronovius
Developer

I’m not able to reproduce the issue.
Can you maybe add the plugin ‘Query Monitor’ and analyze the Main query on the page to see if there are not DB errors? Feel free to share the query so I can have a look with me (support@admincolumns.com)

3 years, 8 months ago
thomas.diesenreiter

Hi stefan, thanks for comming back to me. Here’s the query:

SELECT SQL_CALC_FOUND_ROWS wp_posts.*, COUNT( acsort_postmeta.meta_key ) AS acsort_metacount FROM wp_posts LEFT JOIN wp_postmeta AS acsort_postmeta ON wp_posts.ID = acsort_postmeta.post_id AND acsort_postmeta.meta_key = 'terminelink_termin_location' WHERE 1=1 AND wp_posts.post_type = 'locations' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'archive' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY acsort_metacount ASC, wp_posts.ID ASC LIMIT 0, 20

3 years, 8 months ago
Stefan van den Dungen Gronovius
Developer

Thanks for the query.
I don’t see anything odd about the query.
Can you run it and see if it indeed gives a result with sorted posts (sorted on the last column from the query).
At this moment, I think that the actual value of the column might be incorrect, or that we made a mistake in the way we made the query.

I’m not able to reproduce the issue on my local environment though as it gives me the expected results.

3 years, 8 months ago
thomas.diesenreiter

Yeah, I tried the exact query in phpmyadmin (except the limit clause of course), and all values in the acsort_metacount are 1, so there is no sorting. Just to be sure: There are posts which should have higher values, it even outputs the right number in the column it self. Its just the sorting mechanism thats broken.
It would be cool, if you could fix that.
Thanks!

3 years, 8 months ago
thomas.diesenreiter

Yeah, looking at the query I don’t think it can work that way. It does a single left join and tries to count the meta_key not the meta_value! The value of acf relation fields are stored not as multiple postmeta values, but as a serialized array in a single postmeta entry.
So, yeah, this needs a serious rework.

3 years, 8 months ago
Stefan van den Dungen Gronovius
Developer

Just to be sure, you said you tried the custom field column. Did you not use the Advanced Custom Field column? The ACF column should be able to sort on that value. If you’re indeed using the custom field column, then yeah, it will not work and it probably never will work, since our assumption for that custom field display only works if the value is stored in multiple records in the post meta table.

3 years, 8 months ago
thomas.diesenreiter

Hi Stefan,
no, i used the normal custom field column, because the acf column doesn’t have an option to show the total sum of all entries, you can only show all the used listings themselves.
So it seems like this turns into an feature request for the acf pro addon =). It would be great if you could show the total number of post relation ship fields – just like with the non-acf values, but working ;-)

For users of acf this split functionality is a bit non-intiutive.
Thanks!

3 years, 8 months ago
Stefan van den Dungen Gronovius
Developer

Noted :)
I was about to create a ticket for this, when I already saw a feature request for it. Although the repo is private, this how the issue is described.


The ACF column for a relational field has 4 display options for posts: title, id, image, and author. We could a 5th option that would display the total number of items (count).

I’ve updated the issue to implement the sorting strategy for this display option as well. But still, making sorting work for such serialized data will always be quite heavy on performance since sorting must be done in PHP and cannot be accomplished with SQL only. Thanks for your input on this.

3 years, 8 months ago

You must be logged in to reply to this topic.