Problem: I would like to sort a View using a calculated value.
Solution: Unfortunately this isn't a built-in feature because WordPress does not provide methods for querying posts using calculated values.
Instead, you may be able to programmatically set a custom field value, then order by that custom field. For example, let's say your calculated value is 1 or 2, based on whether or not the post status is "published" and the author's ID = 1. You will add a custom field to this post type called "is-admin-published". Then use the save_post hook to set the custom field to be the calculated value whenever you save a post:
function set_is_admin_published_field ( $post_id ) { $post_type = get_post_type( $post_id ); if ( $post_type == 'post' ) { $boolean = get_post_status( $post_id ) == 'publish' && get_post_field( 'post_author', $post_id )== 1; if( $boolean) { update_post_meta( $post_id, 'wpcf-is-admin-published', 1 ); }else{ update_post_meta( $post_id, 'wpcf-is-admin-published' , 2 ); } } } add_action( 'save_post', 'set_is_admin_published_field', 100 );
Then use the View's ordering options to select the is-admin-published field.
Relevant Documentation:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 4 replies, has 2 voices.
Last updated by Vincent 5 years, 7 months ago.
Assisted by: Christian Cox.