Passing data into variables for IF or filter

I think I am having just a mind blank, but I can’t seem to remember / work out how to pass some data and use it as part of a filter.


I use beaver builder to manage my page layouts
ACF for custom fields

Use case:

I have two post types (dataset1 and dataset2)

In each dataset I have custom field that contains a reference number

I want to be able to display related data from dataset2 on the post for dataset1.

for example lets say my current post has a custom field called reference with the content of “1234”

I want to pass that reference of “1234” and use it to query the dataset2 field called reference (please note that the custom fields in each are ACF but not related - just text fields)

The overall expectation is it will display all posts in dataset2 that have the reference field of dataset1 current post.


Hi Chris, since you’re trying to loop through a custom post type and filter that query based on a particular field, you should be able to do that by using the query parameters that are available for post loops. Assuming the data in your custom field is just a text string, you should be able to write something like this so that when you place this markup on a page from dataset1 that has a “reference” field, it only shows posts that have a matching “reference” field from dataset2.

<Loop type=dataset2 custom_field=reference custom_field_value="{Field reference}">
  Fields you want to display from dataset2.

You might find this forum post from Eliot useful in understanding the different ways to filter the items in a query. I’ll probably eventually either make a blog post about that and/or add details to the docs, but I figured I’d share that with you for now in case that’s helpful.

1 Like

Thanks Ben - this worked :slight_smile:

1 Like