0

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?

2 Answers 2

1

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');
4
  • So I can get the posts or custom posts, but when I want to filter custom posts by "addMetaFieldToFilter('custom_field_name', 'custom field value')" it returns a null collection. I've accounted for lowercase/uppercase, but I can't retrieve posts with 'cell_type' that has the checkbox value 'RNA'. I've even tried addMetaFieldToFilter('cell_type, array('RNA' => 'RNA')) but still returns null. What do you think? And thank you!
    – blueseal
    Sep 15, 2014 at 16:59
  • Change ->load(); to ->load(true); and the SQL will be printed to the screen. This should help see what the query is actually doing. Sep 15, 2014 at 20:31
  • if i have a custom field of select type(ie) drop down) , then my custom_field_name is 'type' and wat will be my 'custom_field_value' ?? I have my select drop down values like articles: Articles, something: Something
    – Sushivam
    Jun 2, 2018 at 10:35
  • The code doesn't get the choices but it does get the selected values. You will have to write your own code to get the choices. Jun 4, 2018 at 10:11
0
$posts = Mage::getResourceModel('wordpress/post_collection')
    ->addIsViewableFilter()
    ->load();

foreach($posts as $post) {
    echo $post->getId() . '<br/>';
    echo $post->getPostTitle() . '<br/>';
    echo $post->getMetaValue('your_custom_field_name') . '<br/><br/>';
}

This code gets all posts and displays the post ID, post title and the value for the custom field that has the bame of 'your_custom_field_name'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.