Question

How to retrieve recent posts on the basis of its corresponding category in fishpig wordpress magento integration?

Was it helpful?

Solution

$numPostsToShow = 2;
$categoryId = 1; //Replace with your category id
$recentPostCollection = Mage::getModel('wordpress/post')->getCollection()
    ->addIsPublishedFilter()
    ->addCategoryIdFilter($categoryId)
    ->setOrder('post_date', 'desc')
    ->setPageSize($numPostsToShow)
;

EDIT

The Fishpig Wordpress module registers the current wordpress category as 'wordpress_category'

So to answer the question in the comments as to how to get the current wordpress category dynamically:

Mage::registry('wordpress_category');

The full example above would then become:

$numPostsToShow = 2;
$categoryId = Mage::registry('wordpress_category')->getId();
$recentPostCollection = Mage::getModel('wordpress/post')->getCollection()
    ->addIsPublishedFilter()
    ->addCategoryIdFilter($categoryId)
    ->setOrder('post_date', 'desc')
    ->setPageSize($numPostsToShow)
;

But you should probably be using the Fishpig_Wordpress_Block_Category_View block which will give you access to $this->_getPostCollection() from within your template that essentially does everything above - why would you be coding this yourself when using the fishpig module?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top