$wpdb选择每个帖子的所有元数据

时间:2016-01-27 作者:Djave

我正在运行以下查询,试图获取大量帖子的数据和相关元数据:

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
返回以下内容:

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
        )
...
但现在我意识到我需要一个更复杂的查询,因为我想要的是以下内容:

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,
            )
        )

2 个回复
SO网友:Jeroen

这真的很简单。

get_post_meta($postid);
这将以大致相同的语法返回所有值。

在更可行的循环中:

<?php
    $custom = new WP_Query(array("posts_per_page" => -1))
    if( $custom->have_posts() ) {
        while ( $custom->have_posts() ) {
            the_post();
            $data = get_post_meta(get_the_ID());
        }
    } else {
        /* No posts found */
    }
?>

SO网友:CᴴᵁᴮᴮʸNᴵᴺᴶᴬ

对于任何一个绊倒在这里的人。。我通过以下操作解决了此问题:

(根据您的代码进行了调整)

SELECT wp_posts.post_title, wp_posts.post_date,
wp_posts.ID, wp_postmeta.post_id, wp_postmeta.meta_key, 
GROUP_CONCAT(wp_postmeta.meta_key SEPARATOR \':\') AS meta_keys

FROM wp_postmeta
INNER JOIN wp_posts
ON wp_postmeta.post_id=wp_posts.id
WHERE wp_posts.post_status = \'publish\'
GROUP BY wp_postmeta.post_id
LIMIT 5
通过这样做GROUP_CONCAT(wp_postmeta.meta_key SEPARATOR \':\') AS meta_keysGROUP BY wp_postmeta.post_id 它创建了一个结果meta_keys 然后可以在PHP中展开:

$meta_keys = explode(\':\', $item->meta_keys);

相关推荐

$wpdb->GET_RESULTS()足够安全吗

原始问题如果我使用get\\u results()函数,这对mysql注入攻击足够安全吗?global $wpdb; $wpdb->get_results("select * from tableA where B = C"); 还是应该先做些准备?最佳做法是什么?在2021 12月的今天,我们应该如何做到这一点?更新,所以我应该用这个来代替?global $wpdb; $my_variable = "sometext"; $p