Site icon Hip-Hop Website Design and Development

$wpdb select all meta for each post

I’m running the following query trying to get the data and associated meta data of a huge number of posts:

SELECT wp_posts.post_title, wp_posts.post_date,
    wp_posts.ID, wp_postmeta.post_id, wp_postmeta.meta_key
    FROM wp_postmeta
    INNER JOIN wp_posts
    ON wp_postmeta.post_id=wp_posts.id
    WHERE wp_posts.post_status = 'publish'
    LIMIT 5

And that returns the following:

Array
(
    [0] => stdClass Object
        (
            [post_title] => Hello world!
            [post_date] => 2015-11-11 08:44:54
            [ID] => 1
            [post_id] => 1
            [meta_key] => _edit_lock
        )

    [1] => stdClass Object
        (
            [post_title] => Home
            [post_date] => 2015-11-11 08:44:54
            [ID] => 2
            [post_id] => 2
            [meta_key] => _wp_page_template
        )

    [2] => stdClass Object
        (
            [post_title] => Home
            [post_date] => 2015-11-11 08:44:54
            [ID] => 2
            [post_id] => 2
            [meta_key] => _edit_last
        )
...

But now I realise I need a much more complex query, as what I’d like is something like the following:

Array
(
    [0] => stdClass Object
        (
            [post_title] => Hello world!
            [post_date] => 2015-11-11 08:44:54
            [ID] => 1
            [post_id] => 1
            [meta] => array(
                '_wp_page_template' => 'home.php',
                '_edit_last' => 2015-11-11 08:44:54,
            )
        )

    [1] => stdClass Object
        (
            [post_title] => Home
            [post_date] => 2015-11-11 08:44:54
            [ID] => 2
            [post_id] => 2
            [meta] => array(
                '_wp_page_template' => 'home.php',
                '_edit_last' => 2015-11-11 08:44:54,
            )
        )

    [2] => stdClass Object
        (
            [post_title] => About us
            [post_date] => 2015-11-11 08:44:54
            [ID] => 3
            [post_id] => 3
            [meta] => array(
                '_wp_page_template' => 'home.php',
                '_edit_last' => 2015-11-11 08:44:54,
            )
        )