0

I need to get the selected post type of a post in wordpress.

I am using Fishpig module, so wp default way of getting the article type wont work.

I am following this: https://fishpig.co.uk/magento/wordpress-integration/code-examples/

But not able to get the post type.

Im trying by this:

UPDATED:

If i try with this below line, i get custom fields values

addMetaFieldToFilter('type', array('Articles','Artists & Makers','Videos'))

But if i use it with $collection, i wont get other details related to posts like post author, post title etc

<?php $collection = Mage::getResourceModel('wordpress/post_collection')
    ->setOrderByPostDate()
    ->addIsViewableFilter()
    ->addPostTypeFilter(array('post')) // Add any post types here
    ->setPageSize(9) // Limits to 9 posts. Remove from all posts
    ->load();
?>
    <?php if (count($collection) > 0): ?>
    <ul>
        <?php foreach($collection as $post):
            echo '<pre>';echo 'POSTS---';   print_r($post)
            ?>

            <li>
                <a href="<?php echo $post->getPermalink() ?>"><?php echo $post->getPostTitle() ?></a>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Output:

POSTS---Fishpig_Wordpress_Model_Post Object ( [_metaTable:protected] => wordpress/post_meta [_metaTableObjectField:protected] => post_id [_eventPrefix:protected] => wordpress_post [_eventObject:protected] => post [_metaHasPrefix:protected] => [_meta:protected] => Array ( )[_metaKeysChanged:protected] => Array ( )[_resourceName:protected] => wordpress/post [_resource:protected] => [_resourceCollectionName:protected] => wordpress/post_collection [_cacheTag:protected] => [_dataSaveAllowed:protected] => 1 [_isObjectNew:protected] => [_data:protected] => Array ( [ID] => 110258 [post_author] => 2 [post_date] => 2017-10-23 11:21:27 [post_date_gmt] => 2017-10-23 11:21:27 [post_content] =>

But if print_r the $collection , i dont get the post_type value like i have in the drop down 'articles'

There seems to no post type in this

These are my custom fields of type select with different values.

enter image description here

And i need to get the selected type value for each post (In this case the type selected is 'article')

enter image description here

  • What's echo $collection->getSelect(); giving you? – Ricky Odin Matthews May 31 '18 at 9:52
  • Have posted the output for getSelect() @RickyOdinMatthews – Sushivam May 31 '18 at 10:28
  • try replace addFieldToFilter with addTaxonomyFilter('*') – Ricky Odin Matthews May 31 '18 at 11:05
  • not working..getting error – Sushivam May 31 '18 at 11:23
  • Also im trying with ths: ->addMetaFieldToFilter('type', 1) can u please tell what should be used in place of 'type', from this reference link: stackoverflow.com/questions/31603961/… @RickyOdinMatthews – Sushivam May 31 '18 at 11:36
0

Try this but not sure

<?php $collection = Mage::getResourceModel('wordpress/post_collection')
    ->setOrderByPostDate()
    ->addFieldToFilter('*')
    ->addIsViewableFilter()
    ->addPostTypeFilter('post')
    ->setPageSize(9); 
?>
  • No i get sql error – Sushivam May 31 '18 at 8:02
  • please post the error. – MR.R DNATH May 31 '18 at 10:12
  • have posted the error @MR.R Dhath – Sushivam May 31 '18 at 10:28
  • Syntax error or access violation, check your query ....May be semicolon. I have made this mistake during post. Please check current update code. – MR.R DNATH May 31 '18 at 10:32
  • I noticed that semicolon in your previous post, but i have corrected it in my code and its currently the same as u have posted and also have posted the output for $collection->getSelect() query – Sushivam May 31 '18 at 10:35
0
<?php $collection = Mage::getResourceModel('wordpress/post_collection')
    ->setOrderByPostDate()
    ->addIsViewableFilter()
    ->addPostTypeFilter(array('post')) // Add any post types here
    ->setPageSize(9) // Limits to 9 posts. Remove from all posts
    ->load() ?>
<?php if (count($collection) > 0): ?>
    <ul>
        <?php foreach($collection as $post): ?>
            <li>
                <a href="<?php echo $post->getPermalink() ?>"><?php echo $post->getPostTitle() ?></a>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>
  • I need to get the custom fields - type (drop down choices )value for each of the post , have updated the question with what you have posted, im stuck as to why im not getting the choices value @Ben Tideswell – Sushivam Jun 2 '18 at 10:51
  • Can you please suggest to get my requirement something like u have mentioned here: stackoverflow.com/questions/25791577/… – Sushivam Jun 2 '18 at 10:52
  • Hey @Ben Tideswell, could you please suggest on this? – Sushivam Jun 4 '18 at 5:30
  • I have done. Stop posting in this question to ask for replies to another question. – Ben Tideswell Jun 4 '18 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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