WordPress Product Review Database: Custom Post Types & More

Building a Product Review Database with WordPress Custom Post Types
Previously, we explored the creation of a basic events listing utilizing WordPress 3.0’s robust custom post types functionality. Following several requests for expansion on this topic, this article details the development of a comprehensive product review database.
This system will allow for the independent management of products through custom post types. Each product entry will include an accompanying image and pertinent details.
Key Features of the Database
The database will incorporate essential meta-information for each product. This includes price, user rating, and detailed specifications.
Furthermore, we will implement methods for displaying these reviews in both a sidebar widget and on a dedicated index page.
Implementation Steps
The goal is to create a streamlined process for managing and showcasing product reviews. This will enhance the user experience and provide valuable information.
We will leverage the knowledge gained from the previous tutorial to build upon the foundation of custom post types. This ensures a cohesive and efficient workflow.
This approach offers a flexible and scalable solution for any website requiring a dedicated product review section. It allows for easy updates and modifications as needed.
Are you ready to proceed? Continue reading to learn how to implement this system.
Prerequisites:
A functioning, self-hosted WordPress installation version 3.0 or later is required. This process involves significant PHP coding. The necessary code will be provided, but a reasonable level of familiarity with PHP and XHTML is expected for customization of variable names or stylistic adjustments.
Should you require an introductory course on CSS and XHTML, a complimentary beginner's guide is available for download.
Theme Considerations:
It is highly recommended to utilize a cleanly coded, yet basic WordPress theme. The default Twenty Ten or Twenty Eleven themes, while popular, possess a degree of complexity that may hinder initial integration.
Therefore, testing the implementation on a simpler theme is advised before attempting to incorporate it into more elaborate designs.
Establishing Custom Post Types
Should you have reviewed the previous week’s instructional guide, a foundational understanding of crafting a custom post type within WordPress should already be present. Implement the following base code into a newly developed plugin, activate it, and commence the process of incorporating new products to establish a dataset for practical application.
(Note: Should an immediate implementation of the complete code be preferred, bypassing the step-by-step addition process, a finished code version is available for use. However, following the tutorial and subsequent customization remains a viable learning path.)

It is also prudent to determine at this stage the specific metadata you intend to link with each product. For example, a database focused on digital cameras might require the following details:
- Retail Price
- Resolution
- HD Video Capability
- Purchase Link
- Rating
Instead of including this information directly within the product description (the 'post content'), we will utilize custom fields to store this data. Ensure custom fields are enabled on the product addition screen.
Then, create a new field for each data set. This field creation is a one-time process; subsequent product additions will allow selection from a dropdown menu. Remember to assign a featured image, as it will be used for display alongside the product information.
Customizing Single Product Displays
Attempting to access a product at this stage may result in a 404 - Not Found error. This can be resolved by navigating to the permalinks settings within WordPress and simply clicking Save Settings. Following this, viewing a newly created product entry should display a basic layout, though it may appear somewhat unrefined depending on the active theme.
The title and description will be visible, but the custom meta information and the featured image will be missing. To address this, we must create a customized template file named single-products.php. This is best achieved by duplicating the existing single.php file, providing a solid foundation for our modifications.
Template Modification
A minor adjustment will now be made to the line displaying the author and date. It will be altered to read “Added to the database on (date)”. This serves as a quick verification that the template is functioning correctly; a refresh of the single product page should immediately reflect this change.
To incorporate the featured image associated with each product, a single line of code is generally sufficient (styling information is also included for convenience). The complete code for my single-products.php file is available for reference, but it’s important to remember that it is tailored to my specific theme and direct copying may yield unexpected results.
Adding Meta Information
The most straightforward method for displaying meta information involves utilizing the following code snippet:
However, this approach produces a basic list of key-value pairs. More complex displays, such as star-rating graphics, require retrieving all values and then iterating through them.
Iterating Through Custom Fields
[View the code here]:
In the example provided, each custom field name (represented by $key) is checked to determine if it matches ‘Level’. If a match is found, a different graphical element is displayed based on the field's content. For all other custom fields, the value is echoed alongside the field name, mirroring the functionality of the_meta().The resulting single product page will then appear as follows:
Further CustomizationFurther refinement of the single product view is largely dependent on the specific theme being used and the desired outcome. For the purpose of this guide, we will now proceed to create a sidebar widget designed to showcase the three highest-ranked products within the database.
Widget Implementation
The following details a modification to previously shared code, originally presented in the article "How to Write a Basic Wordpress Widget." Instead of showcasing a single random post, the code has been refined to display multiple posts.
The complete code can be found [here].
Displaying Multiple Posts
This revised code is designed to present three posts in a layout similar to the example screenshot provided. It's crucial to verify that your products are being displayed correctly.
Carefully inspect the section containing "&meta_key=Rating" to confirm the existence of a meta key with that exact name within your system.
Customization Options
The code is configured to display meta information linked to each product, alongside its featured thumbnail. However, this specific code block can be altered to showcase any desired data.
The resulting display will resemble the screenshot below, offering a visually organized presentation of your product posts.
This allows for a flexible and tailored presentation of product information within your WordPress site.
Product Listings and Archives
A dedicated index or archives page was also created to display a comprehensive list of all products when visiting http://yourdomain.com/products/. This functions similarly to a standard blog index.
The excerpt and post thumbnail style, previously detailed in the "How To Add Post Thumbnails To Your Theme" article, proved largely adequate. However, further customization was achieved by duplicating the archive.php file within the theme and renaming it archive-products.php.
Should an archives page not currently exist, simply duplicate the index.php file and rename it to archive-products.php. Modification of the article meta-info line, coupled with the inclusion of a the_meta() call, yielded the following result:

The current presentation, featuring both archives and a sidebar, may appear somewhat unconventional. Additional styling adjustments are recommended, but are left to the user’s discretion.
This concludes the current demonstration. The complete source code is available online for copying and pasting, or can be downloaded as a file named products.php and placed within your plugins directory.
With this implementation, expanding your WordPress blog into a database of various items becomes feasible. While providing individualized support for specific issues is challenging, please feel free to post questions or feedback in the comments section.
Sharing your results, a tweet, a Facebook like, or a mention on your blog would be greatly appreciated. Thank you for reading, and remember to explore our other WordPress tutorials!