Skip Navigation

[Resolved] How to get the highest custom field value from all CPT?

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

Problem: I would like to show the largest numeric value from a custom field applied to my custom post type.

Solution: One way to do this in Toolset is to create a View of all Books, ordered by your custom field "price" (descending), and limited to 1 result. In the Loop Output you could use the wpv-post-field shortcode to output the value of your price custom field. Then apply the raw text filter to strip out the extra markup, and the View will return the value of the highest custom field. If you don't have that raw text filter, I'll include it here:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
 
function prefix_clean_view_output( $out, $id ) {
  $ids = array( 1, 2, 3, 4 );
  if ( in_array( $id, $ids ) ) {
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

Modify $ids to include a comma-separated list of the numeric IDs of any View you would like to filter to output the raw Loop Output with no extra markup.

Finally, you may need to disable "Don't include current page in query result" in the View editor.

This support ticket is created 7 years, 1 month 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 toolset-dave 7 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#589575
highest-amount.png

Hi, for my Custom Search Price Slider I need to get the highest amount from the field type Number "price" of all the CPTs called "book".

#589636

One way to do this in Toolset is to create a View of all Books, ordered by your custom field "price" (descending), and limited to 1 result. In the Loop Output you could use the wpv-post-field shortcode to output the value of your price custom field. Then apply the raw text filter to strip out the extra markup, and the View will return the value of the highest custom field. If you don't have that raw text filter, I'll include it here:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
  $ids = array( 1, 2, 3, 4 );
  if ( in_array( $id, $ids ) ) {
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

Modify $ids to include a comma-separated list of the numeric IDs of any View you would like to filter to output the raw Loop Output with no extra markup. Let me know if you have follow up questions about this approach.

#590003

Hi, I have placed the code into functions.php, but it gives me error: "Parse error: syntax error, unexpected ';'" on line: if ( in_array( $id, $ids ) {

#590005

Typo on my part, there should be another closing parenthesis on that line. I've updated my code above to reflect that. Can you try that update?

#590129

Hi, thanks for correction of code. It works. The view didn´t work for me, it displayed zero, but I find out that if I disable "Don't include current page in query result" it works nice.