Solved: Problem with Nesting Loops

This turned out to be a problem with a CPT definition. I created a new CPT and moved my posts to it and the problem went away.

FWIW, we did discover that id= works with both ID or slug.

I noticed that you’re using the id parameter on both of your loops. In the docs, it mentions that this parameter is used to “Get posts by ID(s).” Unless I’m mistaken, WordPress post IDs are always a string of numbers, which means that “my-quote” and “my-tidbit” probably aren’t valid IDs.

@benjamin That’s interesting. However, I have confirmed that switching references to the actual numeric IDs works exactly the same. I’ve found a workaround for the original problem which makes that moot for me, but now I’m focused on the ID issue.

Feeding slugs to the id parameter works just as well as numeric IDs in the dozens of templates where I do that. I’d like to know if that’s intentional in which case I can continue doing so since slugs are far more readable than numbers.

If it is intentional, perhaps the documentation should be changed to reflect that. If not I’ll need to get busy to comply with strict IDs.

Thanks, Richard

Oh interesting, you’re right, I’d never thought to try that but the id parameter for some reason does seem to accept post names/slugs. I suppose it was built this way since it increased potential compatibility without really causing any risks of collisions or anything like that.

Still, it seems a bit unnecessary to use that loophole when you could instead use the name parameter which is intended to accept a post name/slug. I guess functionally for you it wouldn’t make a difference, but it would make it more clear what the parameter is referencing.

I’d be curious to know what your workaround was for the issue you were describing. I still don’t fully understand the issue since you mentioned that you “want to take the article content above and put it into a different CPT called Tidbit” but then your template just below seems to loop through a tidbit, which sounds like you’re instead putting a piece of content from a tidbit inside an article, which is the opposite of what you described.

Not sure how I got started on that. Maybe because name= is used in the shortcode that calls the template, so I didn’t want to confuse that. I just transitioned all my loops to search for name.

Basically, I was trying to apply atomic design principles. Quotes are short pieces of text. Tidbits include multiple quotes. Articles include multiple Tidbits.

The act of inserting Tidbits into Articles was causing the display of articles to not show the Featured image. I’m not sure why. Maybe because:

  • A Beaver Themer theme is corrupted (been known to happen).
  • Both the Tidbit and the Article have a featured image.
  • Something to do with the Beaver Themer preview problem discussed elsewhere here.

The analysis of this issue is complicated by cache. Sometimes a change shows without clearing cache, and sometimes a change doesn’t show in spite of clearing the many levels of cache, only to pop up a day later.

My workaround is to bypass tidbits in this instance. It was a marginal use case, and I’m not going to force atomic design only for the sake of it.

Cheers, Richard