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.
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:
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:
There is a difference when you get the collection from:
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.
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
– 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. 🙂
– 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.
– filter by term ID and the term type
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!