How to bring back Post Categories in P2

We’re all excited about the new features in Automattic’s P2 Theme v1.4.0 – however many of us have setup custom tweaks around Post Categories. In the latest version these have been replaced with Post Formats.

Post Formats are great, but they are limited to values defined in the WordPress core. If you wanted to create a new post menu like “critical” or “alarm” this wouldn’t work. We could use Custom Post Types to add to those formats, however it’s way too big an operation for I wanted to achieve: which is bringing back the “posting into categories” feature we’ve come to love from the previous version.

In this article I’ll show you how to back-port categories so posts will appear as they did in P2 v1.3.3 while retaining all functions of the latest update.

I’ve explained how to tweak labels for the post menu in an earlier post – instructions for adding and amending labels are still the same. Here we’ll delve right in to adding a few statements to the P2 code.

Let’s focus our attention on /inc/ajax.php file which contains our function that creates a new post (line 239 in P2 version 1.4.2). We’ll leave everything intact here. Have a look at this array first:

What’s missing here is a category definition so let’s add one to the bottom:

This is pretty much what P2 v1.3.3 did. Just above this block we need to grab the post format and set it as our category, then convert the category slug to a numeric value so that we can use it in this array. Only takes two lines of code (one if you make it more complex) – put them just above the array block:

Now P2 will look at the Post Format (say ‘status’) and add your post to a category with the same name.

Note: If your category does not exist then the post will appear in your default category  as defined under Settings – Writing. So if you’re posting in “Quote” then you need to setup a category by that name manually.

Here are some screenshots to illustrate the benefits:

I’ve added my own “Testing” menu item on the front page
In the backend all posts are now in their respective categories again – as well as retaining their post format properties

This tweak will give you everything the new and improved P2 has to offer while still giving you access to all your category amendments and queries.

 

Full Project Code

UPDATE March 2013:

Somehow my code got screwed up over time in this post. I’ve corrected it so that special characters are displayed correctly. But just in case they are not, here’s a gist of the complete block you need to replace:

I have also created a full working version of this theme with tweaks on GitHub. Feel free to pull and fork as you please:

Enjoy!

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

27 thoughts on “How to bring back Post Categories in P2


  1. <a id="thoughts" class="post-format-button"class="selected" href="" title="">
    <a id="culture" class="post-format-button" class="selected" href="" title="">
    <a id="news" class="post-format-button"class="selected" href="" title="">

  2. What I’d like to be able to do is assign all of the P2 posts into an existing custom post type, e.g. Status Updates.

    I’m integrating P2 with a parent theme, so I’m hacking the theme as best I can.

    1. Hi Chris,

      the answer lies in a file called post-form.php. This generates the buttons above the post box and sets the post type for each button. Look for an unordered list towards the top of the file and change them to your desired format.

      One thing about P2 child themes: they don’t seem to work properly, I’m not sure why. I’ve noticed that some modifications you make in your child theme don’t make it into the final output.

      1. Hi Jay,

        My oversight: I accomplished posting to a special post_type by adding a hidden post_type input field.

        Now if only I could get the p2_load_entry to work correctly. 🙂

Add your voice!