Sorting by field_number

Is it by intention that sorting by number does not show post without a value in the order field?

<Loop type=post></Loop>

returns f.e. 40 items,

<Loop type=post orderby_field_number="my_field" order="desc"><Loop>

returns 10 items (Only the ones having a value in “my_field”)

This does not happen with orderby_field

It sounds like this is probably a bug, but I’m not sure which is the correct behavior. I’ve sent out the bat signal to get some answers from a dev. I’ll let you know what the game plan is. I suppose if orderby_field doesn’t work for your number field, you could trying using one of the more general attributes for the loop tag like sort_field with sort_type=number.

@PolarRacing A recently revived old thread here made me realize that when using the orderby_field query parameter, there seems to be an attribute that can be used called hide_empty=true that hides items that don’t have any value in the field that’s being used to filter them. You could try using that attribute but my assumption, given the fact that this doesn’t seem to be documented, is that this might have been a one-of feature added to the orderby_field parameter but might not have been extended to work with the other attributes. I’ll have to check in with the devs to see what the best approach here is here to create the maximum flexibility across the board. Just thought I’d chime in with that newfound information.

If this is by intention I just have to set the default value for the field to “0” and the sorting works fine - so no big deal. Just want to avoid such “none” values if possible.

Looking into how order_by_field_number works, internally it’s converted into a parameter for WP_Query called meta_value_num, which in turn uses WP_Meta_Query with meta_type set to NUMERIC to construct an SQL statement to query the database and sort by field value.

So, the way it handles empty fields with value NULL is dependent on WordPress internals and the resulting MySQL query, which is impossible (or very tricky/difficult) to modify from the L&L plugin.

As Ben suggested above, using sort_field with sort_type=number can be more flexible, because it runs after the database query is performed. Looking into its code, it seems that an empty field value is treated as 0 (zero) - but if not, we can improve its behavior to control how it works.

As for hide_empty, I found that it’s only implemented in the taxonomy term loop type, and it works by filtering out terms that have no associated posts.

Thanks for looking into.

As I said, I have no issue with that and I see it in some ways even as benefit - I just wanted to know if its by intention or technical reasons. Its not a big deal to set default value to 0 - then custom_field works fine.
Its even a benefit if both approaches works a bit different as its eliminates the need to write a query eliminating zero values if needed and leaves the flexibility to sort with both.

1 Like

Right, this makes sense. Re-reading your comment in the thread I linked, I can see that you explained this parameter there already. Not sure why I thought it had something to do with orderby_field. Thanks for clearing this up :slight_smile: