How to retrieve recent posts on the basis of its corresponding category in fishpig wordpress magento integration?
Asked
Active
Viewed 3,996 times
1 Answers
8
$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?

Drew Hunter
- 10,136
- 2
- 40
- 49
-
Thanks for the reply. But the problem is that I want to retrieve the current category dynamically. – Nida Aug 02 '12 at 10:53
-
What current category? Magento categories and wordpress categories are not related - Can you provide more information on the context here i.e. exactly what are you trying to achieve, where are you putting this code etc – Drew Hunter Aug 02 '12 at 11:05
-
I am telling about blog category. I have created categories in blog and added posts to the various blog categories. In Magento I am displaying posts category wise and they are displaying fine as well. Now the problem is I have to retrieve the most recent posts as per blog category. It is simply displaying recent posts from any category. I want to know how would I retrieve blog category dynamically so to retrieve its recent posts. – Nida Aug 02 '12 at 11:11
-
Thankyou very very much. You just made my day. Thanks once again. – Nida Aug 02 '12 at 11:24
-
@Drew Hunter : If we want to get categoryId in post_view then what to do ? I tried ur trick it didn't worked in post_view page : Mage::registry('wordpress_category')->getId(); as well Mage::registry('wordpress_category'); – Nikhil_K_R Apr 10 '13 at 11:23