Demac Media Demac Media Brand Demac Media Brand



Mini-Tutorial: Full Fishpig WordPress Integration with Custom Post Types and Fields for Magento

Timer Icon 5 Mins

Magento Commerce, Magento Tutorials

There are many eCommerce sites that integrate WordPress for their blog. Many vendors have been integrating WordPress as part of Magento to promote their product on the blog. Some of them even use WordPress as their CMS management system and ditched Magento’s CMS because of the flexibilities. We usually use Fishpig’s WordPress integration for that. However, the extension only loads posts and pages by default. Fortunately, you can override the default behavior with post or/and collection functions.

The following example uses Fishpig WordPress 3.x

Example Load By Id:

Retrieve a custom type post by ID.

Let’s say you want to retrive an “author-profile” post type by ID/Slug. You would normal do the following:

$post = Mage::getModel('wordpress/post')->load($postId);

However, WRONG! The post will not load. The reason for this is that the post model abstract set the post type to “post” if the post type is not specified. Therefore, in order to load the author-profile, you need to use:

$post = Mage::getModel('wordpress/post')->setPostType('author-profile')-load($postId);

Collection Examples:

You are able to filter post by various filter function from various wordpress reousrce models.

Fishpig_Wordpress_Model_Resource_Post_Collection is the main collection class for posts. It inherites the following 2 classes:
– Fishpig_Wordpress_Model_Resource_Post_Collection_Abstract
– Fishpig_Wordpress_Model_Resource_Collection_Abstract

There is a difference when you get the collection from:
– Mage::getResrouceMode(‘wordpress/post_collection’)
– Mage::getModel(‘wordpress/post’)->getCollection();

The later one automatically adds the default post type (‘post’ and ‘page’) into the filters. In my opinion, Mage::getResourceModel is better for initialization since you have total control of the collection.

$collection = Mage::getResourceModel('wordpress/post_collection');

And the you can add various filters to fit your need.


addArchiveDateFilter($archiveDate, $isDaily = false) e.g addArchiveDateFilter(‘2013/09/10’)

– filter by date in ‘yyyy’,’yyyy/mm’,’yyyy/mm/dd’ format. The isDaily flag forces the date to behave in ‘yyyy/mm/dd’ format, if you use ‘2013/09’ with the flag set to true, it won’t return anything.

addCategoryAndPostIdFilter($postIds, $categoryIds, $operator = ‘OR’) e.g addCategoryAndPostIdFilter(array(1,2,3),array(1,3));

The following is the offical comment.
“Filters the collection by an array of post ID’s and category ID’s. When filtering by a category ID, all posts from that category will be returned. If you change the param $operator to AND, only posts that are in a category specified in $categoryIds and $postIds will be returned”

addCategorySlugFilter($categorySlug : string)
– filter by a category slug

addTagIdFilter($tagId : int)
– filter by Post Tag Id

addCategoryIdFilter($categoryId : int)
– filter by a category id

P.S. addTagIdFilter and addCategoryIdFilter function are essential function calls to addTermIdFilter() in Fishpig_Wordpress_Model_Resource_Post_Collection_Abstract with term type specified.


addAuthorIdFilter($authorId : int)
– filters by author ID

– filters by whether the post is published

addPostDateFilter(dateStr : string) e.g addPostDateFilter(‘2013-12’)
– filters by whether the post date. % is required, otherwise the filter treat your input as a standard query array or absolute comparision string.

addPostParentIdFilter(postParentId : int)
– filters by whether the post parent id

addPostTypeFilter(postType: array|string)
– filters by post type

addSearchStringFilter($word : array, $fields : array, [operator = ‘OR’])
– filters the collection by an array of words on the array of fields. This seems allow you to search in all fields by words in post table. (excluding the custom value)

I don’t have a use case for this. If you have worked with this, please comment below. Thanks. 🙂

addStatusFilter(status: array|string)
– filter by post status, e.g publish, schedule, trash and etc.

addTermFilter(term, type : string, field = ‘slug’) e.g. addTermFilter(array(‘cat1′,’cat2′),’category’)
– filter by one of the field in the term tables, the type can be post_type / category or any other value in taxonomy column in taxonomy table.

addTermIdFilter($termId, $type)
– filter by term ID and the term type

Fishpig_Wordpress_Model_Resource_Collection_Abstract class

addMetaFieldToFilter(metakey, filter: string | array) e.g. addMetaFieldToFilter(‘custom-val’,array(1,2,3))
– filter by custom value with one or multiple values.

Multiple Filters Collection Example:

The follow collection the post based on termId, a MetaField and post type filters.

$collection = Mage::getResourceModel('wordpress/post_collection')
->addTermIdFilter(array(1,2,3),'category') //Matches any term in the array, the term may be category, post_tag and etc.
->addMetaFieldToFilter('wpcf-featured-on-category',1) //Matches custom field value
->addPostTypeFilter(array('type1','type2')) //Matches the post type in the array

$collection->getSelect()->order(new Zend_Db_Expr('RAND()')); //This randomizes the selection

I hope the above examples are useful to you. Stay tuned for future posts for Magento WordPress integration tutorials!

Subscribe to the Blog

Stay updated with 20,000+ eCommerce leaders in our community

Sign Up

Let’s talk about your 10X return

Icon/Social/Phone Contact us for pricing information