• Resolved Nox

    (@profnox)


    Hi,

    I’m currently developping a custom wordpress theme for a company which organize used cars sales.

    I’ve created some sortable columns in the admin edit panel, for a custom post-type that I named “Vehicle”, in order to show the date of sell and the date of purchase for each vehicle. Thoses data come from two ACF date fields. I made both columns sortable with the manage_edit-{post-type}_sortable_column function and the pre_get_posts action.

    Until there, no problem. All is working fine. But for the next stape, I’m technically stucked : I’ve created a third column, which calculate the difference between the two dates, in order to show the “stock duration” of the vehicle (in days). So I got a column which show results like “0 day”, “1 day”, “13 days”… (screenshot here, I translated it with the red labels).

    However the pre_get_posts action waits for a query.

    I wonder how could I make this “Stock Duration” column sortable, considering this one isn’t a meta_key, it’s a result coming from a specific treatment : the difference between two dates and a word “day” (or “days”) after it.

    Is there a solution to simply order them by this kind of treatment ?

    Thank you in advance.

    • This topic was modified 5 years, 11 months ago by Nox.
    • This topic was modified 5 years, 11 months ago by Nox.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Nox

    (@profnox)

    Oh my god… I just realized that the pre_get_posts hook was just an optional stape.
    Native sort behaviour seems to do the job.

    Tell me if I’m wrong, but, it automatically sorts the posts depending on the “pure” result (the string shown in the column DOM), in the alphabetically order ?

    Moderator bcworkz

    (@bcworkz)

    The default sort for posts is reverse chronological. When you click a sortable column, generally the same query is redone with only a different ORDER clause. By using the “request” filter in conjunction with “manage_edit-{post-type}_sortable_column”, you can set new query vars in addition to “orderby”, so the new query could actually return completely different results. Examples here.

    Generally speaking, we are limited to what can be done through WP_Query::query_vars. However, I believe we could set other WP_Query::get_posts() filters to further alter the eventual query. So adding a filter to “posts_request” from within the “request” filter callback would let us completely alter the query run. I’ve not done this myself, but it seems feasible.

    I’m rather weak with SQL, but I think some sort of calculation query could be composed that orders results by a calculation on two fields. Set this query in the added “posts_request” filter and you should be good to go.

    Thread Starter Nox

    (@profnox)

    Hm, so I’d try to play with the “posts_resquest” filter if I come across a harder case.
    Here the default behaviour with the column order clause seems to work, so I’ll avoid bulky code to do the same sorting in my case.

    Thank you for your help and those details.

    • This reply was modified 5 years, 11 months ago by Nox.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sortable columns : Order by a treatment ?’ is closed to new replies.