• I’m trying to find out how I can enable sorting in the admin area on a custom post type. I’ve got the columns worked out but I can’t get the sorting to work the way that I need.

    The problem that I’m facing is that even though the value is stored as a meta_key, what I’m storing as that value is the ID of another custom post type, and I need to be able to sort of the post_title of that second custom post type.

    As an example…

    I have a post type of ‘Products’ and ‘Manufacturers’. I store the manufacturers ID as a meta value for the product and I show the manufacturers name when listing the products. I need to be able to sort on the manufacturers name, not ID. As it’s the ID that’s stored in the database as the meta key, that’s all that I can find that I can sort by.

    Is there any way of adding a more custom sorting method that would support what I’m trying to do?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I take it you’ve gotten as far as being able to sort the column by the ID meta_key? Done by adding query vars in the ‘request’ filter, right? From there we’re limited to setting query vars that could have been defined as arguments to new WP_Query objects. AFAIK, you cannot sort by manufacturer name (as you’ve described the relationship) by using WP_Query arguments alone, it’s one level of abstraction beyond WP_Query’s capabilities. If you could, it’s then a matter of setting the same query vars as you would for WP_Query.

    I’d be surprised if it’s that simple. In a regular query to sort by manufacturer name, as I see it, we would need to hook into the ‘posts_*’ filters of $wp_query->get_posts() and join in the tables necessary to relate manufacturer name to the ID. I’m assuming the name to ID relationship exists in the DB in some manner, yes? If not, this sort is truly not possible. If yes, I believe we can still hook into the ‘posts_*’ filters to modify the query as needed for column sorts just as we do for regular queries of the same type. You can still check for your column name in the ‘orderby’ query var just as you would in the ‘request’ filter.

    I’ve not ever hooked ‘posts_*’ filters for a column sort query, but I believe it is a viable approach. Good luck!

    Thread Starter catacaustic

    (@catacaustic)

    I have the manufacturer ID sved in the post_meta table so it’s possible for it to be retrieved. I’m not sure about the filters that you’ve said, so I’ll have to see just what I can do. I’d need to join the tabl esomehow, and that’s where I’m a bit stuck. I could do this in plain SQL in seconds, but trying to work in with the query object… not quite so easy from what I’ve seen.

    Moderator bcworkz

    (@bcworkz)

    Yeah, that’s the thing, I don’t think it’s possible to get the joins you need through the WP_Query object. (Do take a look though, it’d be better if you could) That’s where the ‘posts_*’ filters (starting line 2602 of query.php) come in. Another thing you could do is a wholesale SQL query replacement via ‘posts_request’. Then the query vars are a mere stand in and have no effect on the results. A shame to go through all the parse query code just to throw it out and paste in something else, but I can’t imagine any way to bypass that without re-doing the list table class.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Sort admin grid by custom column value’ is closed to new replies.