How to create a loop of specific post IDs?

Hi there, I wanted to ask for some help on the required syntax to achieve the following:

  • Lets say I want my loop to just be 3 specific Post IDs, these are 6826,21639,7429

I have tried putting in this code to my test page:

<Loop id="6826,21639,7429">
  <Field title />
  <Field width="650" image />
</Loop>

But all that gets displayed is the title and image from the current test page (and not the data from the post id’s in my loop

Please can someone tell me what I am doing wrong?

Thanks in advance for your help,

Keith

Hi Keith,

The loop just needs a bit more context by including type=post, so that it does not default to the current post:

<Loop type="post" id="6826,21639,7429">
  <Field title />
  <Field width="650" image />
</Loop>
2 Likes

Thanks very much for replying :slight_smile:

So I have another question in that case, how do we query more than 1 post type at a time? Or is that not possible?

Cheers,

Keith

I thought I’d chime in to mention that technically, Loop type=post specifies a “post” loop-type, which happens to default to using the “post” post-type. This still totally works, but the recommended way to go about specifying a post type is with the post_type query parameter like Loop type=post post_type=post or the shorthand, Loop post_type=post. Any of these approaches achieve the same thing, but when creating post loops that refer to other custom post types, it’s better to use post_type in case you happen to have a CPT on your site that has the same name as an L&L loop type, such as a CPT named user or something like that.

Which brings us to this:

You’ll notice that in the docs, each of the query parameters mentions which type of data it accepts.

  • post_type - Post type(s)
    Type: string, array

I’ve been meaning to rename this to make it clearer, but any time it says “array” it means this query parameter accepts an array, i.e. a comma-separated list. Same as that id parameter you’ve got. So all that to say that if you want to work with multiple post types, it’ll look a little something like this:

<Loop post_type="post,product,my_cpt" id="6826,21639,7429">
1 Like

ahhhh right - I get it. That’s awesome, thanks for explaining in such detail, it really helps as a new user.
Really appreciate your help

2 Likes