For all enquiries please call or email us. 01484 506220info@designthatfits.co.uk

Fishpig Wordpress - Magento 2 - Filtering getPosts

Fishpig's Wordpress extension was a key part of any magento 1.9.x project. This hasn't changed with Magento 2, but the documentation available could do with some improvement.
Aug 14, 2017
Fishpig's Wordpress extension was a key part of any magento 1.9.x project. This hasn't changed with Magento 2, but the documentation available could do with some improvement.

We've worked with Fishpig's Wordpress extension on plenty of Magento 1 projects, but unfortunately when we moved over to Magento 2 we found the documentation to be lacking.

Here is a quick tutorial on how to implement some basic filtering. In this particular example, we were looking to show only "post" taxonomies, set the page size to 3, then set the current page to 1 so that we could load up just the 3 most recent posts for a block on our clients homepage.

To start with we created a block to appear within the CMS, set it to use Fishpigs "ListPost" class, and then to select our own template so that we could make the appropriate post modifications

{{block class="FishPig\WordPress\Block\Post\ListPost" name="wp.post.list" template="post/dtflist.phtml"}}

We then created our new template file "dtflist.phtml" within: app/design/frontend/Namespace/Theme/FishPig_WordPress/templates/post/ and copied over the default list.phtml template from the FishPig extension.

After examination of the ListPost class and those higher up the inheritance, we were able to find some of the class methods that we could filter by, and then make adjustments to suit our project.

Our dtflist.phtml file starts with the usual $posts = $this->getPosts();, we then expand upon this with the following:

$posts->setPageSize(3); //set the number of items per page.
$posts->setOrderByPostDate(); //orders our posts by the post date (latest first)
$posts->addPostTypeFilter("post"); //filters the taxonomy to only show "posts" - otherwise pages and other custom taxonomy can also appear.
$posts->addIsPublishedFilter(); //only show published posts
$posts->setCurPage(1); //set the current page
$posts->load(); //reload the posts based on our filter.
$posts->clear();

The rest of our dtflist.phtml file works as normal with a foreach loop over the $posts object.

Return to articles
Fishpig's Wordpress extension was a key part of any magento 1.9.x project. This hasn't changed with Magento 2, but...
Marketing automation is a great asset for any business and once set up requires very little maintaining.
If you use plesk to run Magento 2 you might find difficulty when you come to run bin/magento commands
We'd suggest adding address lookup to the checkout.

Businesses can trust us to deliver effective online marketing solutions. Find out why.

Speak to an expert