Have you ever wanted more control over how posts are categorized in WordPress? We did a project recently where the client wanted a WordPress story authoring site. He wanted to use normal categories for genres such as horror, sci-fi, etc. But he also needed to sort them by storylines because the premise for the site was to have several possible storylines all stemming from the same first chapter but with different chapters after. This means there is actually several separate stories with only the same first chapter in common. For this to work, we needed to be able to call up posts by two categories (genre & storyline) instead of one category as WordPress allows by default. Learn how we did this by reading this tutorial which demonstrates how to use custom fields and how to create customized WordPress pages.
Creating a Custom Field
Custom fields are easy to use and have a wide variety of different uses. If you are using WordPress version 3.3.1 or higher, your custom fields are no longer displayed by default in WordPress. All you need to do to activate them is to click on “Screen options” in the upper right corner of the “add post” page and check the box before “allow custom fields”. Then there will be a custom field option box under the main text editor.
What is a custom field?
Custom fields consist of a name/value pair A.K.A. key/value pair. Generally you use the name or key like a database field name so you have one name and several values for it(for different posts). The value field would be like the data stored under the field name. For our story authoring site, we named our custom field “storyline” and it is used to specify the story name that the chapter or post(same thing) is being written for. There are many other uses for custom fields, but for this tutorial we will concentrate on this one common use.
Imagine you have a WordPress blog about games and you wanted to categorize the posts according to the skill levels of the games in posts. The only problem is that you also want to categorize the posts according the game type. You used the main category functionality in WordPress to determine the type of game the post is about. Now you need to create a custom field to make the other category work. You could simply use the custom field named “skill” and give it a value of easy, intermediate or difficult. That would allow us to easily pull up and display all of the posts according to the skill custom field. Can you see how it would be a huge convenience to be able to categorize posts in more than one way?
In the following exercise we are going to build a custom WordPress page that will list posts according to a specific custom field. We will create a custom page with a drop down for users to select all posts of a specific category. This method can be used to create a subcategory for your posts. You could use custom fields to add any categorizing method you like to your posts or pages. You could organize them by topic, quality, author, subject, content or anything else you desire.
For our purposes we are using the default theme, but the same instructions would apply for almost any theme. We are going to make a quick example WordPress site to demonstrate how this is done. For this exercise we start with a fresh install of WordPress 3.3.1 using all default settings and default theme. If you want to follow along, install a new WordPress installation first. Here are the steps to building a site with more than one category
Adding Posts and Custom Fields
If you don’t have any posts to categorize for this exercise, go ahead and create at least three posts now so you can follow along with the exercise. From the WordPress admin section, go to the first post you want to categorize and access it in edit mode.
Under the text editor, add a new custom field named “skill” and give it a value of “easy” or use an appropriate name and value for your purposes. If the custom fields option box is not there, read how to solve this issue above where we discussed custom fields.
Repeat the last 2 steps at least two more times so we have at least three categories. Use the same custom field name by selecting it from the dropdown after the time. Then add values of “intermediate” and “difficult” or use your own categories. Now you should have at least three posts with three different custom field values under one key or name.
Starting a Custom Page
For this exercise to work, the next thing we need to do is start a custom WordPress page. To do this, open your favorite text editor and start a new PHP script called custom_category.php or any name you see fit.
Add the name for your custom theme. The name you choose here will show up under the templates drop down of the WordPress admin when you add a new page. We named ours “Custom Category”. Here is the format you have to follow in order for WordPress to recognize the new template. Add the following two lines to your blank PHP file you started:
Now from your WordPress admin, go to “Pages” and select “Add new” to start a new page. Give the page an appropriate title for the page you are making to show posts by a new category. We named ours “Skill Levels”. Make sure you do not enter anything in the text editor for this page because we are adding the content in our PHP file.
In the right column of the WordPress admin, locate the templates drop down and you should find your new page template listed there. Select it and press “Publish” to make the page live.
Click on “View Page” at the top of the WordPress admin to make sure your test text displays in the WordPress page correctly. If you see your text there with the regular site header and footer, then congratulations, you just created another child theme for your WordPress site!
Using the WordPress Database to Access Custom Field Data
Next we will demonstrate how to use built in WordPress database functions to access custom field data in order to display your new categorized posts.
First, go back to your PHP script you began earlier for your custom page template and add the following line to initialize the WordPress db class that makes accessing the database possible using WordPress functions. Delete your temp text and add this in it’s place:
Now we need to access the custom fields to make a dropdown list of all the categories we made earlier using custom fields. In our case we used skill levels. To access the database to get a list of all these new category names, we will use the get_col WordPress function. Start by adding the following under where you initialized the db class in the last step. The only thing you may need to change is where we have “skill” for meta_key. If you didn’t name your custom field “skill”, then type what you named yours there instead:
Now add in your html form with the dropdown box to show your new categories like in the following code example:
Save and test your page at this point by uploading it to your WordPress site and refreshing the page. You should see a dropdown with all of the custom field values you added earlier.
Now build your query string to call up posts according to the chosen category as in the following example code:
Now add your own version of a WordPress loop to show the posts for the category that was chosen from the dropdown. Add the following code:
That is really all there is to it. You may need to make some minor changes to make it work for your exact needs, but it is fairly simple to convert to anyone’s situation.
This tutorial demonstrated one use of WordPress custom fields, it’s now up to you to think of other new and existing ways you can use them to expand the functionality of WordPress to fit your needs. Custom Fields have many other possible uses including inserting code snippets, dynamic data and various MySQL database uses. Good luck with your next custom WordPress project and keep on coding with WordPress!