Display custom posts filtered on the basis of a custom field

Hi
I have created a custom post type called Projects and have been able to create the following loop to display a list of ALL posts:

<Loop type=project orderby=date order=desc>
<a href="{Field url}" alt="{Field title}"><Field image /></a>
<h4><a href="{Field url}" alt="{Field title}"><Field title /></a></h4>
<p><Field excerpt /></p>
</Loop>

but I would also like to display lists of projects on the basis of custom fields I have set up in the custom post:

  1. I have created a true/false field type with the slug ‘featured-project’ and would like to create a template that only display posts if the field is ticked (ie, true)

  2. I have also created a text field type with the slug ‘project-country’ and would like to create a template that only display posts of one particular country.

I’m guessing (but am not convinced this is right!) that I would start by adding the field name to the loops, like this:

<Loop type=project field=featured-project orderby=date order=desc>

<Loop type=project field=project-country orderby=date order=desc>

but I can’t work out how to specify ‘true’ for the first option and a specific country for the second option.

I hope that all makes sense!

Thank you for any help you can give (or if you have a better way of achieving the desired end result)

A couple things / questions:

1 - How are you setting up the fields? ACF?

2 - Based on your description, project-country should probably be a taxonomy, not a custom field. Is there are reason you have it as a custom field?

You can try:

<Loop type=project custom_field=project-country custom_field_value="YOUR VALUE" orderby=date order=desc>

or

<Loop type=project field=project-country field_value="YOUR VALUE" orderby=date order=desc>

In this case custom_field should do the trick.

Hi Zack

Yes, ACF. And, I think you’re right. Initially I hadn’t intended to filter the posts by country but now I am so I will try taxonomies instead. Good idea.

However, regarding the true/false field type I have set up to select whether it is a featured project or not, this sounds like a sensible approach to me - except I can’t get it to work!

I tried these in the loop, as suggested by Ralf (thanks Ralf):

field=featured-project field_value=“1”
custom_field=featured-project custom_field_value=“1”
field=featured-project field_value=“true”
custom_field=featured-project custom_field_value=“true”

but none of those worked.

Can you think of anything else please? I’m wondering if there should be an if/else element to it? If true then display, if not don’t?

For info, this is the ACF doc about true/false: ACF | True / False

Many thanks

Thank you for your suggestion Ralf - please see my reply above :slightly_smiling_face:

Hello again. I’ve now changed the project-country custom text field to a taxonomy as discussed above but I am completed confused about how to add this to a template to:

A. display the country of a project in this loop:

<Loop type=project orderby=date order=desc>
<a href="{Field url}" alt="{Field title}"><Field image /></a>
<h4><a href="{Field url}" alt="{Field title}"><Field title /></a></h4>
**<h5>DISPLAY COUNTRY HERE</h5>**
<p><Field excerpt /></p>
</Loop>

and B. display only posts from specific countries using a version of the above loop.

I’ve been struggling with this all day so am feeling a bit :scream: !!

(And I haven’t got anywhere with the true/false thing above either…!)

I’m very sorry for my ignorance

Show Taxonomy:

<Loop type=project orderby=date order=desc>
<a href="{Field url}" alt="{Field title}"><Field image /></a>
<h4><a href="{Field url}" alt="{Field title}"><Field title /></a></h4>
**<h5><Taxonomy YOUR_TAXONOMY_NAME><Field title /></Taxonomy></h5>**
<p><Field excerpt /></p>
</Loop>

Loop:

<Loop type=YOU_TYPE taxonomy=YOUR_TAXONOMY_NAME terms=YOUR_TERMS >....</Loop>

I decided to test this on my end this morning just see if things were working as expected. For starters, I just wrote <Field true_false /> on my template to see what the field itself contained. On my test site, it seemed that when the field was set to true, the value of the field was 1 and when it was false, the value of that field was 0. Is that what you see when you try this on your installation?

Based on that information, I tried creating a filtered loop that looked like this, which worked as expected:

<Loop type=post custom_field=true_false custom_field_value=1>
  <p><Field title /></p>
</Loop>

I tried this too, which also worked:

<Loop type=post field=true_false field_value=1>
  <p><Field title /></p>
</Loop>

Unless your true/false field is outputting something different than mine (you can check using the method mentioned above), I assume this should work for you too. My only hypothesis about that issue is that in the template you wrote, you seem to have directional quotation marks, like this “ ” instead of regular straight quotation marks like this " ". Is that your issue?

That worked perfectly. Thank you so much Ralf, I really appreciate your help. It looks so easy and obvious now!

1 Like

And thank you Benjamin. I managed to get it to work with these 2 options (using the name of the true/false field type instead of true_false):

custom_field=featured-project custom_field_value=1
field=featured-project field_value=1

The only difference between this and what I tried yesterday is that there are no quotation marks around the value. Could that be why it didn’t work before? I didn’t use directional quotation marks last time.

Thanks again for helping me find a solution :blush:

Glad it worked! The fact that removing the quotation marks worked seems to indicate that your quotation marks were some non-standard character, although without seeing your exact template I can’t know for sure.

I bet if you copy-pasted this into your template it would work too: field="featured-project" field_value="1"

You’re right! I was clearly having a bad day yesterday!
Thanks again Benjamin