Create a Loop for a single item by ID

I want to create a loop to grab fields from a single item which could be one of several post types. I’m sending the id of the item to the template as a variable, e.g. pid="12345"

This isn’t working:

<Loop custom_field="id" custom_field_value="{Get local=pid}" >

Cheers, Richard

Unless you’re piggybacking off the default query of the current page, you always need to specify a loop type. I think you should be able to list multiple post types in an array if you’re not querying just one post type.

1 Like

Well stated. On your Loop Docs page I suggest you replace the first sentence under Loops with the sentence above.

After much testing, I “discovered” that custom_field doesn’t work for CPTs. Your docs say as much,

Both the `attachment` and `post` loop types include support ...

With the wisdom of hindsight, I understand that a “custom post type loop” is not a “post loop type”, but it would have helped me for the docs to explicitly state that CPTs don’t work with custom_field.

Cheers, Richard

This isn’t correct. The post loop applies to “post , page , and custom post type.” I’m away from my computer at the moment so I can’t test whether that parameter is working as expected, but I thought I’d clarify this.

Sorry if I misspoke. I was referring to the use of custom_field

The docs seem to say CPT loops don’t support custom_field, and that was my experience, but maybe I’m wrong.

To clarify, this didn’t work

<Loop type="people" custom_field="id" custom_field_value="{Get local=pid}" >

I could get this to work

<Loop type="people" field="id" field_value="{Get local=pid}" >

And I also got this to work, although I couldn’t find where the docs allow it

<Loop type="people" id="{Get local=pid}" >

Done, thanks for the suggestion.

What I meant with my clarification above is that all attributes that work with a type=post loop also work with type=page and type=some_custom_post_type. So a “custom post type loop” is a “post type loop” and they share all the same query parameters, including custom_field.

I got around to testing this myself today and you’re right, it seems that the custom_field attribute doesn’t seem to work on core fields like id or name. It does seem to work for actual custom fields though, which is really what it’s designed for since core fields already have specific attributes built in that allow for a shorter template. Is there a reason you’re trying to use custom_field=id custom_field_value=something or field=id field_value=something instead of just id=something?

When I was testing that, I looked at the doc for loop and couldn’t find anything that said you could use id=something

I’m not saying it isn’t there. Maybe it’s implicit, or I just missed it. I eventually found id=something in an unrelated example.

Maybe on the loop page under Attributes you could mention the core fields that can be used as attributes.

Thanks. Richard

I’m confused. You’ve used the id query parameter in several of your own posts, like the one here, here, or here.

I think you might want to re-read the documentation for the Loop, which now has much better explanations about the difference between attributes of the Loop tag (which are available for all loop types) and query parameters (which are unique to each loop type). Specifically this part:

Each loop type has its own query parameters that can be used, so see the links under loop types below for the parameters available in each type

In this case, you’re working with a post loop, so you can take a look at the list of query parameters available for the post loop to see what’s available.

1 Like

Ha. Clearly, I’m the one who is confused. When I came back to this I had just spent several days using a new markup language (MJML) that apparently wiped out all other knowledge.

2 Likes