Question

I have Magento with Fishpig_Wordpress module. I've created some new postmeta data for posts, that is saved at postmeta table. I just saw that Fishpig has a custom load SQL method in /Model/Mysql4/Post.php..

protected function _getLoadSelect($field, $value, $object)
{
    $select = $this->_getReadAdapter()->select()
        ->from(array('e' => $this->getMainTable()))
        ->where("e.{$field}=?", $value);

    if (Mage::getDesign()->getArea() == 'frontend') {
        if (Mage::helper('wordpress/plugin_allInOneSeo')->isEnabled()) {
            foreach(Mage::helper('wordpress/plugin_allInOneSeo')->getMetaFields() as $field) {
                $table = 'aioseop_'.$field;
                $select->joinLeft(
                    array($table => Mage::helper('wordpress/db')->getTableName('postmeta')), 
                    "{$table}.post_id = e.ID AND ".$this->_getReadAdapter()->quoteInto("{$table}.meta_key=?", "_aioseop_{$field}"),
                    array('meta_'.$field => 'meta_value')
                );
            }
        }
    }

    $select->limit(1);

    return $select;
}

That uses the Mage::helper('wordpress/db')->getTableName('postmeta') in the joinLeft method. But I don't know how if I should use the _getLoadSelect protected method or create another class to call the postmeta table.

So, the question is: Is there a way to get data from postmeta table with Fishpix module or I need to create a new class for this?

Was it helpful?

Solution

For any one reading this now, you can retrieve a postmeta value using the following code:

<?php echo $post->getMetaValue('your_meta_key') ?>

This same method can be used for any WordPress entity that has a meta table (posts, pages, comments, users etc)

OTHER TIPS

I solved the problem above by mapping the postmeta table under config.xml as below:

<entities>
    ...
    <post_meta>
        <table>postmeta</table> 
    </post_meta>
</entities>

And by creating a new method under Fishpig/Wordpress/Model/Post.php:

public function getPostMeta(Fishpig_Wordpress_Model_Post $post, $meta_key)
{
    if($post->getId() == '')
        return '';

    $table =  Mage::helper('wordpress/db')->getTableName('postmeta');
    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');

    $query = "SELECT meta_value FROM {$table} WHERE post_id = 0" . $post->getId() . " AND meta_key = '" . $meta_key . "'";

    return $readConnection->fetchOne($query);
}

So I can use it on the frontend by calling:

Mage::getModel('wordpress/post')->getPostMeta($post,'facebook');

I guess it's not the best way for doing this, but let me know if you get someway better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top