有一种方法可以在 Magento/Wordpress(带有 Fishpig 扩展)中获取自定义字段的所有选中复选框:
$post->getCustomField($customfield)
我正在尝试通过选定的复选框过滤帖子,并且正在考虑通过 for 循环将过滤器与帖子进行比较,但是有没有更有效的过滤帖子的方法?
您可以创建由自定义字段过滤的自定义帖子集合:
$posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->addMetaFieldToFilter('custom_field_name', 'custom field value')
->load();
这将返回名为“custom_field_name”的自定义字段的值为“自定义字段值”的所有已发布帖子。
一旦你有一个帖子模型,检索自定义字段值的正确方法是使用以下内容:
$customFieldValue = $posts->getMetaValue('custom_field_name');
$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/>';
}
此代码获取所有帖子并显示帖子 ID、帖子标题和具有“your_custom_field_name”的自定义字段的值