How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display on a website?

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!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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" rel="nofollow noreferrer noopener">%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="/" rel="nofollow noreferrer noopener">Home</a></li>
    <li><a href="http://wordpress.stackexchange.com/" rel="nofollow noreferrer noopener">WordPress Stack Exchange</a></li>
    <li><a href="http://wordpress.org" rel="nofollow noreferrer noopener">WordPress.org</a>
        <ul>
            <li><a href="http://wordpress.org/download/" rel="nofollow noreferrer noopener">Download</a></li>
            <li><a href="http://wordpress.org/about/" rel="nofollow noreferrer noopener">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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply