Those Crazy Custom Posts

On one of my projects, I’ve made heavy use of the Custom Post Type of WordPress.  I’ve seen a lot of folks refer to it as good business if you’re into turning the majority of site maintenance/publishing over to the client.  In fact, to be honest, all of my recent projects bar one have been using custom post types in some way, shape, or fashion.

The problem I have with this one project is that the custom post types have to be read inside of the regular “posts loop”.  Most WP sites recommend that you add a filter to the theme functions (or plugin file, if you’re going that route) that makes sure that CPT’s are injected into the loop query.  That looks something like this:

While that’s all fine and good.  I ran into an issue with the Phoenix Site.  I use 3 custom post types (technically two I created and two that are loaded via plugin).  The two that I created are one to control an image slider (as part of the theme, make it easier to swap out images) and a CPT to handle the weekly newsletters and keep them separate from the regular blog/news posts.  The other two are “Fiat Alert” (for flashing a box on the page) and of course, the calendar.

I tried modifying the above function by changing around the “$query->set (‘post_type’, $post_types )” line, but always seemed to create more headaches and not a workable solution.

The issue popped up when trying to view the Category archives for the newsletters.  I didn’t create a custom taxonomy just for the newsletters, instead, letting them pull into the “main” categories.  Silly me, I know.  What happened, is that because Fiat Alert queried the database “the right way” (using new WP_Query), I was getting one of the newsletters popping up in the alert div, then of course, repeating in the regular loop.  Try as I might, I couldn’t get it to work correctly.

But then a funny thing happened.  I noticed that I wasn’t getting the same issue with the query that I used on my slides.  I finally realized that I had written the slider query in the “wrong” way using query_posts.  So, quick re-write of the code, and it’s fixed.

Just an FYI, I didn’t edit the plugin itself, but basically re-created the entire plugin inside of my own custom-post types calls and made the edits there.  Given the simplicity of the Fiat Alert plugin, it wasn’t too bad.

So, here’s the original query from Fiat Alert –

And here’s my “bastardized” or “wrong way” –

Near as I can tell, the original problem lies in the fact that WP_Query can be “hooked” into (like it was), which is a normally a good thing.  Sometimes, I guess, you just don’t need to be worrying about the hooks so much.