Support » Plugin: Flexible Posts Widget » 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:

    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:

    /* 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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thanks for this.

    Unfortunately it’s not working for me for some reason. I got a meta key that holds a last name. I want a alphabetic sorting using that meta key value. But the plugin keeps using the setting from the widget itself. How can i overrule that with this?

    • This reply was modified 5 years, 8 months ago by Borneyak.
    Thread Starter ubertam


    If done correctly, it will override whatever the widget setting is set to. My guess is the code I added above isn’t being recognized. Couple possibilities:

    Is your code properly done in a custom plugin or in your theme’s functions.php file?
    If it is a custom plugin, is that plugin enabled?
    If sorting by text rather than numbers, change ‘meta_value_num’ to ‘meta_value’ – that might just be it.
    Make sure you replace ‘hidden_sort_value’ with your proper custom field.

    Used this in a plugin. Got that plugin enabled ofcourse.
    I want to use this for a lastname field i provided.

    I will check out if i can find something.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Order by meta_value_num to sort my list by a custom value’ is closed to new replies.