Order by meta_value_num to sort my list by a custom value
-
I’ve been looking for a long time at a few of these plugins, and they all missed one thing for me. Different things, but always something wasn’t perfect. FPW is the most complete (can specify custom taxonomies and allows custom templates), but it didn’t let me sort by a custom field I had in ACF (Advanced Custom Fields).
Based on this person’s post (and the author’s reply), I was able to get FPW to work for me:
https://www.ads-software.com/support/topic/custom-filed-integration/I have a custom plugin I use to hold functions like this so I don’t deal with my theme’s functions.php, but here is the code I added to get it to sort:
<?php /* Adjust Flexible Posts Widget to order by meta_key */ add_filter( 'dpe_fpw_args', 'pcva_fpw_sortmeta' ); function pcva_fpw_sortmeta($query) { // The value in meta_key is my custom field's name $query['meta_key'] = 'hidden_sort_value'; // The value in orderby is 'meta_value_num', which means sort it as a number. 'meta_value' would sort it as alphanumeric. $query['orderby'] = 'meta_value_num'; return $query; }
I’ve been looking for this a long, long time, and since I got it working, I thought I’d share.
- The topic ‘Order by meta_value_num to sort my list by a custom value’ is closed to new replies.