Skip Navigation

[Resolved] Adding Fields to View Sorting Controls Sorting Options

This support ticket is created 4 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 7 replies, has 2 voices.

Last updated by Jamal 4 years, 5 months ago.

Assisted by: Jamal.

Author
Posts
#1687079

Tell us what you are trying to do?

Allow users to filter view results by fields that aren't included in the Sorting Options drop down, specifically Post Status.

Is there any documentation that you are following?

I've tried looking through the following:
https://toolset.com/course-lesson/creating-a-custom-search/
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/

What is the link to your site?

Currently this section requires login

I have a View that displays content created by the currently logged in user and amongst the key information fields that are shown are the Title, Post Type and Post Status. I want the user to be able to filter their content by any of these three fields as there is an approval process behind submitted content so it would be useful for them to be able to see which of their posts are Awaiting Approval or Rejected etc.

Both Title and Post Type are selectable options from the Sorting Options panel in the Sorting Controls sidebar, but there is no option for Post Status I have gone back and watched the training video on the search feature and had a leaf through the documentation, but I cannot find any reference to how to add fields and options to this.

I dare say it's there and I've just missed it, but I've been searching for quite a while now with no luck. Can someone point me at the documentation required of provide an example of how to add Post Status to the Sorting Options for my view please?

#1687403

Hello and thank you for contacting the Toolset support.

Currently, the post status is not an option on the sorting controls. I'll file this a feature request to our 2nd Tier for another evaluation.

In the meantime, I think, that we can hook into the view query and programmatically sort it with post_status. Check this view hook wpv_filter_query https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

And check this StackOverflow question https://wordpress.stackexchange.com/questions/193405/how-to-order-by-post-status

#1688843

Hi Jamal,

Thanks for the reply.

I would have no issue sorting them programmatically, but that wouldn't provide the functionality required. I need to allow them to be sortable from the front end by the user. Surely there is a way to hook into the Sorting Options array and add post status?

Forcing it behind the scenes wouldn't serve any purpose as it needs to be at the users behest. You seem to have hooks for just about everything else, even some that aren't on the documentation such as:

toolset_filter_edit_post_link_extra_statuses_allowed

Any suggestions?

#1690265

I do not know if we had such a filter. But I am sure, that the hook will not be enough, it has to have the corresponding logic that will modify the view query to use this new sorting criterion.

I am approaching our 2nd Tier to get another opinion. I'll get back to you as soon as possible.

#1690267

Ok, thanks. I'll wait to hear from you.

#1691867

We have got an answer from our 2nd Tier, and unfortunately, we can't add the post status to the sorting control.

In fact, post_status is not available as one of the "orderby" options in the WP_Query class, used by Views. That is why it is not available on sorting options, and we won’t offer to add it in future releases.
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

As a workaround, you can use a custom field that will also record the post status, and use that field for sorting. You can sync the value of that field by hooking to post_save.
https://developer.wordpress.org/reference/hooks/save_post/

#1706515

That's really disappointing as it seems like such an obvious field to be able to use.

I haven't had a chance to try and implement your workaround solution yet, but I'm not sure if I have missed a step in what you're suggesting. Obviously creating a field to track the post status isn't a big issue, but can you select custom fields in the OrderBy in the Sorting Controls?

The only thing I'm not really sure about is why I'd need to sync the value for front end sorting controls as the post status wouldn't be changing. This is where I feel like I've missed something.

#1706603

I understand the frustration that you may felt about us not implement this sorting with post_status, but, as our developers have confirmed, it is not supported by WordPress, so we won't support it too.

can you select custom fields in the OrderBy in the Sorting Controls?
Yes, we can sort by custom fields. We can sort by strings and numbers.

The only thing I'm not really sure about is why I'd need to sync the value for front end sorting controls as the post status wouldn't be changing.
I suggested syncing the value of the custom field with the post_status just to avoid having to enter the value for the custom field on each post update(status change). If the status won't change, you won't need to implement any syncing.

I'll remain at your disposal. Let me know if you have any questions.