Filtering a query by field

Hey fellow L&L aficionados!

I’m playing around with filtering my query using the field attribute using the syntax documented here. I’m just playing around with dummy content to better understand how this works, but something isn’t working for me. I have a custom “bicycle” post type and each bike has a custom field called “price” which contains a number value. The following markup works:

<Loop type=bicycle field=price field_type=number field_value=1800>
  <strong><Field title /></strong> $<Field price /><br />
</Loop>

This makes the loop only display two of the bikes which both have a price of 1800:
image (24)

However, when I try adding the field_compare attribute, my loop returns nothing. For example, I tried the markup below which (if my understanding of loop filtering is correct) should loop through all posts of type bicycle that have a price field with a value greater than or equal to 1700. But when I use this syntax, nothing gets displayed:

<Loop type=bicycle field=price field_type=number field_compare=more_than_or_equal field_value=1700>
  <strong><Field title /></strong> $<Field price /><br />
</Loop>

Does anyone have any clues about why this might be the case?

Not sure it’s relevant but the doc mentions custom_field_compare, not just field_compare:

Or maybe by nesting the condition?

<Loop type=bicycle >
  <If field=price field_type=number field_compare=more_than_or_equal field_value=1700>
    <strong><Field title /></strong> $<Field price /><br />
  </If>
</Loop>

Thanks for the tips Emmanuel! I tinkered around with the custom_field and custom_field_compare parameters too but that doesn’t seem to work for me either. Using the not comparison and a value of 1800 doesn’t show any bicycles, but using the after comparison shows all the bicycles, even the ones with a price below 1800. Weird. It’s also a bit strange that the comparison term syntax is different between the custom_field_compare parameter and then field_compare parameter (equal vs is, for example). Wonder what’s up with that.

You’re right that I could also achieve this same kind of thing by just nesting a conditional statement inside a loop (although I believe the correct syntax for that would be <If field=price more_than_or_equal value=1700> and not <If field=price field_type=number field_compare=more_than_or_equal field_value=1700>). I guess I’m just trying to better understand query filtering in case I ever have a use case where I’d want to filter query items at the query level instead of filtering within the inner content of a loop using a nested If statement. I can imagine that if I was trying to save a short query from a massive database using the query variable, that seems like a context where it’d be better to use query filter parameters so that you could avoid having to re-use the same conditional logic every time you wanted to get that query variable.

Hi Ben,

This was a helpful issue description, I found that field_compare was an unfinished feature. The documentation said it accepts all common comparisons from If tag, but that part was not implemented yet.

In the newest plugin version, I connected the dots and filled in the missing code. So the following snippet from your first post should work as expected.

<Loop type=bicycle field=price field_type=number field_compare=more_than_or_equal field_value=1700>
2 Likes