Displaying only the items in a loop that have a certain field filled out

Hey Tangible peeps,

I’m trying to create a loop that only shows posts from a CPT that have a certain field filled out. This is not a taxonomy.

Is this possible?

Hello, and welcome to the discussion forum.

Here is an example of checking if a field is empty or not.

<Loop type=post_type_name>
  <If field=field_name>
    Field has value.
  <Else />
    Field does not have value.
  </If>
</Loop>

Alternatively, there should be a way to create a loop query for non-empty field value, but it’s not supported yet. In the future, we plan to implement something like the following:

<Loop type=post_type_name custom_field=field_name custom_field_compare=exists>
1 Like

Hey there, Eliot!.

Yeah, I’ve had no problems just checking if a field was empty or not, and just not having field show if it was empty. has been straightforward and easy!

Thanks for the info about the future capability of creating a loop query for non-empty fields, that is what I wanted to do. Looking forward to when that is completed! :slight_smile:

This might be a weird and seemingly unrelated question… but is there a way to show the number of posts within a CPT that has a certian field filled out? Maybe using ?

I want something to say “There are _________” with a number dynamically updating depending on how many posts have a certain field filled out.

Hi Michael!

There actually is a way to do this but it’s a bit complicated. You can use the <Math> tag to iterate a variable by one each time the field exists in a loop item:

<Math>total=0</Math>

<Loop type=post_type_name>
  <If field=field_name>
    <Math>total=total+1</Math>
  </If>
</Loop>

<p>There are <Math>total</Math> posts which include field_name</p>

This might mean having to use two Loops if you want the number to appear before your other content.

The math tag is documented here (https://loop.tangible.one/modules/math)

2 Likes

This worked perfectly, Julia! Thank you!

1 Like

Hi Michael - The newly released version 2.0.2 has added support for creating a loop query for non-empty fields.

Something like the following should work:

<Loop type=post_type_name custom_field=field_name>

The attribute custom_field_compare=exists isn’t necessary because it’s the default.

2 Likes