0

How to format the meta_value column's json from the post_meta table in wordpress to get only the proper values.

I have the value something like:

$posttype=  Mage::helper('wordpress')->getPostType2();
$posttype = explode(',',$posttype);
echo 'Post type:';

echo '<pre>';print_r($posttype);

Output:

Array ( [0] => a:12:{s:3:"key";s:19:"field_57bdd83367bb1";s:5:"label";s:4:"Type";s:4:"name";s:4:"type";s:4:"type";s:6:"select";s:12:"instructions";s:0:"";s:8:"required";s:1:"0";s:7:"choices";a:7:{s:7:"Article";s:7:"article";s:9:"Blog Post";s:9:"blog_post";s:16:"Artists & Makers";s:16:"artistsandmakers";s:6:"Videos";s:6:"videos";s:12:"In The Press";s:12:"in_the_press";s:14:"Did You Know ?";s:12:"did_you_know";s:14:"Glossary A - Z";s:8:"glossary";}s:13:"default_value";s:39:"blog_post in_the_press did_you_know ";s:10:"allow_null";s:1:"0";s:8:"multiple";s:1:"1";s:17:"conditional_logic";a:3:{s:6:"status";s:1:"0";s:5:"rules";a:1:{i:0;a:2:{s:5:"field";s:4:"null";s:8:"operator";s:2:"==";}}s:8:"allorany";s:3:"all";}s:8:"order_no";i:0;} )

I need to get only the values like in a dropdown

Article

Blog Post

Artists & Makers

<div class="category-list">
            <?php // echo $this->__('All Types') ?>
            <select id="blogcat">
                <option><?php echo $this->__('Article Type') ?></option>
                <option value=""><?php echo $this->__('All Types..') ?></option>
                <option value="<?php echo $posttype;?>"> </option>
                <?php foreach($posttype as $value) { ?>
                        <?php $namecheck = preg_replace('/\s*/', '', strtolower($value['name']));
                        if ($value['slug'] != 'artists-and-makers'&&$namecheck != 'artistsandmakers') : ?>
                        <option value="<?php echo $value['slug'];?>"><?php echo $value['name'];?> </option>
                        <?php echo 'TRUE';?>
                    <?php endif; ?>
                <?php } ?>  
                    <option value="events"><?php echo $this->__('Events') ?></option>
            </select>
        </div>
2
  • Assuming the $posttype variable is given the meta_value value you want, and assuming it's stored as json, you can do json_decode( $posttype ) (instead of the explode) to convert it to a PHP object or array. May 29, 2018 at 17:32
  • Have updated my question @SeventhSteel, need to format my string to remove the double quotes and anything after that
    – Sushivam
    May 29, 2018 at 17:45

2 Answers 2

2

Part 1 - To remove the double quotes/extra characters:

Instead of trying regular expressions, since the output is somewhat predictable, I would try the following instead of the getCapitalLetters function:

function strip_cruft( $str ) {
    $str = str_replace( '";s', '', $str );
    $str = str_replace( '"', '', $str );
    return $str;
}

Part 2 - To output a populated dropdown:

Use the above function and remember to add a value in between the opening and closing tags:

<?php foreach($posttype as $post) {
    $post = strip_cruft( $post ); ?>
    <option value="<?php echo esc_attr( $post ); ?>">
        <?php echo esc_html( $post ); ?>
    </option>
<?php } ?>

If you'd like them to be all lowercase, you can use WordPress's sanitize_title function.

1

In your original question you had text like this

a:12:{s:3:"key";s:19:"field_57bdd83367bb1";s:5:"label"

This is not JSON. This is a PHP serialized Array. This is a representation of a PHP array intended for storage in a database. It is created with the serialize() function. If you try to update_post_meta() or update_option() with an Array value in WordPress then WordPress will serialize the data first. When you use get_post_meta() or get_option() WordPress will unserialize() the data for you.

Your problem here is that you're not retrieving this data with WordPress functions. You're using Mage::helper('wordpress')->getPostType2(), which appears to be a Magento function. If you're using this data in Magento then A. This is the wrong forum, please ask in a Magento forum if you have issues in Magento and B. You need to unserialize the data yourself:

$posttype=  Mage::helper('wordpress')->getPostType2();
$posttype = unserialize($posttype);
echo 'Post type:';

echo '<pre>';print_r($posttype);

PS: I rolled back your question to the first version, because it was the only version of the question that included information that explained the problem.

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.