2

I would like to retrieve multi-dimensional arrays (that have been created with metaboxes), & display them on the front end of a website using shortcodes.

My Question: How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display them in a shortcode? Do I use extract ?

Thank-you!

  • 1
    This is a very vague question. Are you expecting to be shown how to create a shortcode? There is the codex for that. – vancoder Apr 10 '13 at 18:23
  • 1
    Just research here for get_post_meta and add_shortcode, plenty of good examples. – brasofilo Apr 10 '13 at 19:03
  • maybe his doubt is about the serialize() unserialize() ? – krembo99 Apr 10 '13 at 19:04
  • @krembo99 then you have answered his question :) – vancoder Apr 10 '13 at 19:20
3

When you save an array as post meta, WordPress will serialize it to a string and save that string.

Let’s take a simple array:

array (
    1 => array (
        'url'      => '/',
        'title'    => 'Home'
    ),
    3 => array (
        'url'      => 'http://wordpress.stackexchange.com/',
        'title'    => 'WordPress Stack Exchange'
    ),
    4 => array (
        'url'      => 'http://wordpress.org',
        'title'    => 'WordPress.org',
        'children' => array (
            2 => array (
                'url'   => 'http://wordpress.org/download/',
                'title' => 'Download'
            ),
            5 => array (
                'url'   => 'http://wordpress.org/about/',
                'title' => 'About'
            )
        ),
    )
);

In your database this will look like this:

a:3:{i:1;a:2:{s:3:"url";s:1:"/";s:5:"title";s:4:"Home";}i:3;a:2:{s:3:"url";s:35:"http://wordpress.stackexchange.com/";s:5:"title";s:24:"WordPress Stack Exchange";}i:4;a:3:{s:3:"url";s:20:"http://wordpress.org";s:5:"title";s:13:"WordPress.org";s:8:"children";a:2:{i:2;a:2:{s:3:"url";s:30:"http://wordpress.org/download/";s:5:"title";s:8:"Download";}i:5;a:2:{s:3:"url";s:27:"http://wordpress.org/about/";s:5:"title";s:5:"About";}}}}

Not really pretty, but that shouldn’t bother you: When you call …

$data = get_post_meta( $post_id, 'YOUR_KEY', TRUE );

… WordPress will unserialize that string for you, so you get your array back.

What you do with that array is up to you and not really a WordPress question.

You could write a simple walker and create a list.

The walker function could look like this:

function link_array_walker( &$item, $key, $indent = 1 )
{
    printf(
        '%1$s<li><a href="%2$s">%3$s</a>',
        "\n" . str_repeat( "\t", $indent ),
        $item['url'],
        $item['title']
    );

    if ( ! empty ( $item['children'] ) )
    {
        print "\n" . str_repeat( "\t", $indent + 1 ) . '<ul>';
        array_walk( $item['children'], __FUNCTION__, $indent + 2 );
        print "\n" . str_repeat( "\t", $indent + 1 ) . "</ul>\n" . str_repeat( "\t", $indent );
    }
    print '</li>';
}

And then you call it:

print "<ul>";
array_walk( $data, 'link_array_walker' );
print "\n</ul>";

Result:

<ul>
    <li><a href="/">Home</a></li>
    <li><a href="http://wordpress.stackexchange.com/">WordPress Stack Exchange</a></li>
    <li><a href="http://wordpress.org">WordPress.org</a>
        <ul>
            <li><a href="http://wordpress.org/download/">Download</a></li>
            <li><a href="http://wordpress.org/about/">About</a></li>
        </ul>
    </li>
</ul>

There is also a class Walker in WordPress that does a little bit more. You could write a child class instead of the function above.

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.