12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Dec 13, 2023

Filtering Query Loop Posts by a Non-empty Custom Field in Oxygen

Sridhar Katakam

In the unofficial Oxygen group on Facebook, a user asks:

I’m using a repeater element that pulls a custom post type. This part is all good.

Where I’m getting stuck is that I’m trying to ONLY show posts that have content in one of the custom fields.

For instance: there are 10 testimonials, but only 6 have “short quotes” in a specific field.

How would I edit the query to only show the testimonials with short quotes?

There are 2 approaches to this.

  1. Applying a condition on each Repeater’s Div to check if that post’s custom field value is not empty.
  2. Prefiltering the query to only pull the posts for which the custom field value is not empty.

This article shows how the second approach above can be implemented in Oxygen.

We will use the Advanced Query option and tell WordPress to get posts of the specified post type and a add a meta query that reads “only if this custom field exists and its value is not empty”.

Set up your Repeater and change the WP Query type to advanced.

Then configure it so it looks like this:

Replace post with your post type’s identifier and both instances of `custom_excerpt’ with the name/ID of your custom field.