【问题标题】:Display all the Post and its Post meta of specific custom post type显示特定自定义帖子类型的所有帖子及其帖子元
【发布时间】:2015-04-26 21:28:43
【问题描述】:

有人可以帮我显示特定自定义帖子类型的所有帖子和 Postmeta

我在 wordpress 项目中创建了一个自定义帖子类型“课程”,其中包含一些自定义元框

我需要显示该自定义帖子类型的所有帖子和自定义元框值

我已经尝试了下面的查询。

$query = "
SELECT   $wpdb->posts.ID,$wpdb->posts.post_title,$wpdb->postmeta.meta_key
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE               
$wpdb->postmeta.meta_key IN ('_course_code','_instructor_name')
AND    
$wpdb->posts.post_type = 'course'   
";

$results = $wpdb->get_results($query);       
echo "<pre>"; 
print_r($results);

通过对这个查询结果进行分组,它只给出一个 meta_key 值

它给了我结果:

请注意 id 和 post_title 以新索引显示两次

Array
(
    [0] => stdClass Object
        (
            [ID] => 10
            [post_title] => introduction to wordpress
            [meta_key] => _course_code
        )

    [1] => stdClass Object
        (
            [ID] => 10
            [post_title] => introduction to wordpress
            [meta_key] => _instructor_name
        )

    [2] => stdClass Object
        (
            [ID] => 13
            [post_title] => introduction to hacking
            [meta_key] => _course_code
        )

    [3] => stdClass Object
        (
            [ID] => 13
            [post_title] => introduction to hacking
            [meta_key] => _instructor_name
        )

)

但我期待结果为

Array
(
    [0] => stdClass Object
        (
            [ID] => 10
            [post_title] => introduction to wordpress
            [meta_key] => _course_code
            [meta_key] => _instructor_name
        )

    [2] => stdClass Object
        (
            [ID] => 13
            [post_title] => introduction to hacking
            [meta_key] => _course_code
            [meta_key] => _instructor_name
        )

)

【问题讨论】:

  • 考虑提供与您想要的结果相对应的适当 DDL

标签: php mysql wordpress


【解决方案1】:

我自己设法得到了结果,把我的答案放在这里希望它能帮助别人。

$query = "
            SELECT $wpdb->posts.ID,$wpdb->posts.post_title,
            PM1.meta_value as _course_code,
            PM2.meta_value as _instructor_name
            FROM $wpdb->posts
            LEFT JOIN $wpdb->postmeta AS PM1 ON ( $wpdb->posts.ID = PM1.post_id AND PM1.meta_key = '_course_code')
            LEFT JOIN $wpdb->postmeta AS PM2 ON ( $wpdb->posts.ID = PM2.post_id AND PM2.meta_key = '_instructor_name')
            WHERE $wpdb->posts.post_type = 'course'
            AND $wpdb->posts.post_status = 'publish'
            AND ((PM1.meta_key = '_course_code') AND (PM2.meta_key='_instructor_name')) 
            GROUP BY $wpdb->posts.ID
            ORDER BY $wpdb->posts.post_date DESC

         ";

$results = $wpdb->get_results($query);

echo "<pre>"; print_r($results); die("here");

【讨论】:

    【解决方案2】:

    首先,不要做$wpdb->posts,因为很难阅读。其次,缺少 GROUP BY sql 查询部分。

    $posts = $wpdb->posts;
    $postmeta = $wpdb->postmeta;
    $query = "SELECT $posts.ID, $posts.post_title, $postmeta.meta_key
    FROM $posts
    LEFT JOIN $postmeta ON ( $posts.ID = $postmeta.post_id)
    WHERE         
    $postmeta.meta_key IN ('_course_code','_instructor_name')
    AND  $posts.post_type = 'course'   
    GROUP BY $posts.ID";
    
    $results = $wpdb->get_results($query);       
    echo "<pre>"; 
    print_r($results);      
    echo "</pre>"; 
    

    【讨论】:

    • 在没有任何聚合函数的情况下,group by 子句可能是一个错误。另外,请注意,这呈现为 INNER JOIN
    • 我已经使用了 group by ,但它只提供了 post meta 中的一个 meta_key ('_course_code'),其他的都不见了。
    • 按 ID 分组是因为您的示例。如果您需要查看唯一值,例如 meta_key ,请以分隔的逗号添加到分组中:GROUP BY $posts.ID, $postmeta.meta_key
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多