Support

Account

Home Forums Backend Issues (wp-admin) Make a single value post object sortable (edit.php table column)

Solved

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.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.