Home › Forums › Backend Issues (wp-admin) › Make a single value post object sortable (edit.php table column)
I’ve added a post object column to an admin edit table (edit.php?post_type=test) using this action: add_action( ‘manage_test_posts_custom_column’, …) and marked it as sortable: add_filter( ‘manage_edit-test_sortable_columns’, …).
Then you have to hook into: add_action( ‘load-edit.php’, …) and modify the request filter: add_filter(‘request’, …)
There you have to set the right ‘meta_key’ and ‘orderby’, but this doesn’t work, because the ‘post object’ holds just the post-id of the relation.
Is there any way to order the table by the post object’s title or any other field of this relationship?
Thanks!
You would need to do a query of the posts of that type, getting them all 'posts_per_page' => -1,
and order them by whatever you want to order them by. You can return just a list of IDs by setting 'feilds' => 'ids',
Then you can use this returned array of IDs to order your posts. To do this you set 'post__in' => $return_from_first_query,
and 'orderby' => 'post__in'
.
Thanks for your help!
The problem is, that this solution slows down, the more records there are and at some point the SQL statement could reach the maximum length.
Well, yes, there is that, the solution is not perfect and does not scale well. Another solution would be.
use an acf/save_post filter. When your posts are saved get the title of the post object field and store that in another custom field not controlled by ACF. Then sort based on this field. What this does is it makes the title of the related post a value associated with the post.
A more complicated solution would be to use $wpdb and construct your own MySQL query to do the work, but this solution is beyond me.
use an acf/save_post filter. When your posts are saved get the title of the post object field and store that in another custom field not controlled by ACF
This sounds better. Is there a way to make a field invisible to the backend user?
any field that starts with an _ (underscore) is treated as a hidden field and not shown by WP.
The topic ‘Make a single value post object sortable (edit.php table column)’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.