Loop all posts of a custom post type

Dear Support-Members,

I am wondering how I can loop all available posts of a custom post type regardless of the post state.

If I write:

<Loop type=podcast sort_field=publish_date sort_order=desc count=1 >
  <Field example />
</Loop>

I get the field “example” of the latest published post. I tried it by adding:

status=publish,pending

But still got the field of the latest published post. What am I doing wrong? I need to get the value of a field from the latest post and it doesn’t matter if it is published or a draft.

Thanks for your help! All the best, Daniel

Hey Daniel! Your markup seems correct to me and it works as you’re describing when I test it on my end. You mention that “it doesn’t matter if it is published or a draft” which makes me wonder if you should be using status=publish,draft instead of status=publish,pending. If you swap out that status, does that solve your problem?

Hey Benjamin! Thank you for your fast reply. I just had the time to give it another try and found a solution. In the end I want to get the total number of posts and wrote this snippet:

<Loop type=podcast status=publish,pending,draft,future,private >
  <Get loop=total />
  <If count=2>
  <Exit />
  </If>
</Loop>

With this code I get a number of all posts (only missing the ones in trash), and that is what I want :slightly_smiling_face: Do you have an advice or could I reach my goal in an easier way?

Glad to hear that you’ve got it working with the post statuses you need!

Regarding the markup you shared, I can see why you’ve done it that way. Without that If tag, your markup would display the number associated with loop=total as many times as you have items in the loop. I admire the ingenuity in your solution!

A simpler way to achieve this would be to use the loop=previous_total variable which shows the total number of items in the previous loop after that loop has been closed. So you could simply write:

<Loop type=bicycle status=publish,pending,draft,future,private></Loop>
<Get loop=previous_total />

Sweet! Thanks a lot.