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:
$post_id = wp_insert_post( array( 'post_author' => $user_id, 'post_title' => $post_title, 'post_content' => $post_content, 'post_type' => 'post', 'tags_input' => $tags, 'post_status' => 'publish' ) );
What’s missing here is a category definition so let’s add one to the bottom:
$post_id = wp_insert_post( array( 'post_author' => $user_id, 'post_title' => $post_title, 'post_content' => $post_content, 'post_type' => 'post', 'tags_input' => $tags, 'post_status' => 'publish', // bringing back P2 Categories 'post_category' => array( $post_cat -> cat_ID ) // end of mod ) );
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:
// define $post_cat
$post_cat = $_POST['post_format'];
// and turn it into the category ID
$post_cat = get_category_by_slug( $post_cat );
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!




{ 17 comments… read them below or add one }
very nice… thanks. P2 has a lot of promise
Awesome. Works ! Many, many thanks for this.
Thanks, that’s very useful (Y).
Would be great if I could format the input fields to take recipes…and restaurant locations. I’ve been looking for a recipe theme to do that..I liked P2 from the beginning..if I could make recipe posts and restaurant location posts with geo data from the front I’d be a very happy camper.
I’m having difficulties implementing this fix. Whenever I apply the edits, and then go to my front end I get a pop up telling me I need to login. This happens anytime I click in the status update box.
This is a fresh P2 install, v1.4.
My $post_id = area started around line 237.
Should I just be able to simply paste in your code examples and go or am I missing a step?
Hi Matt,
copy and paste should work fine, but remember you’re amending existing code so make sure it looks like what I’ve shown. Make sure the second block goes above the array block. Here’s what it should look like in its entirety:
On this note, I’ve just discovered that a code highlighter plugin that I had installed last week has screwed up some of the characters. I’ve corrected it now so all is as intended again (albeit without code highlighting). Still works with WordPress 3.4.1, I’ve just tested it.
Thanks Jay. I did see the broken code and replaced it, but I must have overlooked something. Your new code works perfect with no issues.
I was able to do exactly what I wanted, thanks so much.
You’re very welcome, glad it works! I think they should put this into the P2 core for legacy purposes. As much as I love the new features in P2, I really miss this function!
I agree. I have no need for links or blog posts, but I’d like for people to be able to subscribe or post to specific categories that are relevant to them. This solves that. Hopefully the Automaticc will se that eventually.
I get the same errors.
Plus, I want to know if it is possible to change the form styles for each form. I really want to use P2 for a website, but I need it to be more specific to my site, which is to have a restaurant directory and a recipe databank..
Try again Nofia, think there was a code hitch before. I’ve tested it this morning and it worked for me and Matt.
Which forms would you like to style? There’s only really one generic input form, not a lot you can do with it I’m afraid. Works slim and light for me as it is though, so I’m not sure if I can help you there.
Hi Jay, nice post here. I’m now on P2 1.4.2 &WP3.5.1 and encounter same issue as Matt. Are you able to post the sample that you have shown Matt (Here’s what it should look like in its entirety:) as I couldn’t see anything after. I’m still seeing broken characters (> instead of >) and wondering if i’m missing out anything.
Thanks lobong! Not sure how I can help you – I think Matt was happy with the sample code. What is it that’s not working for you, and what would you like to achieve?
Well I have inserted the define $post_cat block directly on top of the $post_id block, but when I came to front end I got an error that says I’m not connected to the server, and I couldn’t post anything from front end. Removing these codes reverts the functionality of P2. So I’m not sure if it’s the latest version of P2 that blocks it or latest WP version, or the fact that I have entered the codes incorrectly. Can you show me exactly how the two block of codes should go?
(Side note, the code snippets above still show broken characters as I see it, for example the character “>” is shown as “&-g-t-;” (without dash)).
Thanks Jay!
Hi lobong, thank you so much for bringing this to my attention – it’s one of those posts in which it’s vital never to switch to the visual editor… I’ve attempted to correct the sample code, hopefully it displays correctly now.
But that’s not all: I’ve also added a gist at the bottom in case this happens again. Even better: you can get a full working version of this mod on GitHub now:
I hope this will help bring category goodness back to your site! All the best
Hey Jay you’re awesome! Works like charm now!
Thanks so much. Am surely following your blog for other tweak tips!
{ 1 trackback }