Skip Navigation

[Resolved] Sort Order By Calculated Field

This thread is resolved. Here is a description of the problem and solution.

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 support ticket is created 6 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
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 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1109006

By default, the sort order fields can only be by Post ID, Author etc.

I am trying to sort order based on the values from [calculated] shortcode with the view plugin.

#1109097

Hi, unfortunately this isn't a built-in feature because WordPress does not provide methods for querying posts using calculated values:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

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.

#1109226

Hi Christian,
thank you so much for the speedy response.

Meaning to say, we save the calculated value in the custom field "is_admin_published" and sort this custom field.

I am calculating the values from a:
Repeatable Group within Value from Editable Group 1 (V1) - Value from Editable Group 1 (V2)
Example: List Price (V1) - Global Discount (V2)

In this case, do I create the custom field "is_admin_published" in the Repeatable Group that is within the Editable Group?

#1109585

In this case, do I create the custom field "is_admin_published" in the Repeatable Group that is within the Editable Group?
If the View you want to sort is a View of the Repeatable Group then, yes, you should add the calculated field in each row of the Repeatable Group.

#1112885

My issue is resolved now. Thank you!