Search code examples
wordpressmagentofishpig

How to Filter by Custom Field in Fishpig?


There is a way to get all the checked checkboxes of a custom field in Magento/Wordpress(with Fishpig extension):

$post->getCustomField($customfield)

I'm trying to filter the posts by selected checkboxes, and I'm considering to compare the filters to the posts via for loop, but is there a more efficient way of filtering posts?


Solution

  • You can create a custom collection of posts that are filtered by a custom field:

    $posts = Mage::getResourceModel('wordpress/post_collection')
        ->addIsViewableFilter()
        ->addMetaFieldToFilter('custom_field_name', 'custom field value')
        ->load();
    

    This will return all published posts that have the value 'custom field value' for the custom field called 'custom_field_name'.

    Once you have a post model, the correct way to retrieve a custom field value is with the following:

    $customFieldValue = $posts->getMetaValue('custom_field_name');