简体   繁体   English

显示特定自定义帖子类型的所有帖子及其帖子元

[英]Display all the Post and its Post meta of specific custom post type

Can some one help me to display all the Posts and Postmeta of Specific custom post type 有人可以帮我显示所有自定义帖子类型的帖子和帖子元吗

I have created a Custom Post Type 'Course' in a wordpress project, with some Custom Meta Boxes 我在wordpress项目中创建了一个自定义帖子类型“课程”,并带有一些自定义元框

I need to display all The Posts and Custom Meta Box Values of that Custom Post Type 我需要显示该自定义帖子类型的所有“帖子”和“自定义元框”值

I have tried the query below . 我已经尝试过下面的查询。

$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);

By Grouping this query results it only gives only one meta_key value 通过将此查询结果分组,它仅给出一个meta_key值

And it's giving me the result : 这给了我结果:

Please notice the id, and post_title being displayed twice with new index 请注意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
        )

)

But i am expecting the results as 但我期望结果

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
        )

)

I have manged to get the results by myself, putting my answer here hoping it will help others. 我努力让自己得到结果,将我的回答放在这里,希望对其他人有帮助。

$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");

First of all, don't do $wpdb->posts, because hard to read. 首先,不要读$ wpdb-> posts,因为它很难读。 Secondly, missing GROUP BY sql query part. 其次,缺少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>"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM