Skip to content

Instantly share code, notes, and snippets.

@logoscreative
Created June 23, 2015 19:01
Show Gist options
  • Save logoscreative/753b888f3664c9635a82 to your computer and use it in GitHub Desktop.
Save logoscreative/753b888f3664c9635a82 to your computer and use it in GitHub Desktop.
Pods meta_value_num PoC
<?php
/**
* WordPress stores all meta fields as strings, which causes problems for ordering by numbers. When using Pods::find() directly, this issue can be addressed by casting the field as a decimal, as shown here: https://github.com/pods-framework/pods-code-library/blob/master/example/classes/Pods/find/examples/orderby-number.php
*
* In shortcodes, this strategy is not possible, as MySQL functions can not be used in the WordPress post editor. Instead, you can use the "pods_shortcode_findrecords_params" params filter, as shown below:
*/
/**
* Example to order by a price field properly.
*/
//SHORTCODE [pods name="ofertas" where="idprogram.meta_value='12011' and sessioncheck.meta_value='1'" limit="50" orderby="price.meta_value" template="oferta"]
add_filter( 'pods_shortcode_findrecords_params', 'slug_orderby_by_number_pods_shortcode', 10, 2 );
function slug_orderby_by_number_pods_shortcode( $params, $pod ) {
if ( isset( $params[ 'orderby' ] ) && strpos($params[ 'orderby' ], 'meta_value_num') ) {
$params[ 'orderby' ] = 'CAST(' . $params[ 'orderby' ] . ' AS DECIMAL)';
}
return $params;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment