选择查询以获取meta_key的结果

时间:2012-01-13 08:10:14

标签: php wordpress

我有一个自定义字段名称" front_sl_slider"。我想显示所有包含自定义字段的帖子" front_sl_slider"值设置为1.另外,我想显示我查询后得到的帖子的缩略图,标题和其他自定义字段数据。

$meta_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE  post_type = 'page' AND  meta_key = 'front_sl_slider' AND meta_value = 1 ORDER BY menu_order", 'OBJECT'); 
    if ( $meta_pages ) 
        {
        foreach ( $meta_pages as $pageChild1 ) : setup_postdata( $pageChild1 );
         echo get_post_meta($pageChild1->ID, 'front_sl_slider', true);
         echo get_post_meta($pageChild1->ID, 'project_client_name', true) ."<br>";
         echo get_post_meta($pageChild1->ID, 'project_architect', true) ."<br>"; 
        endforeach; 
        }

1 个答案:

答案 0 :(得分:3)

$querystr = "   SELECT $wpdb->posts.* , $wpdb->postmeta.*
                        FROM $wpdb->posts, $wpdb->postmeta
                        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
                        AND $wpdb->postmeta.meta_key = 'Featured_Projects_Page' 
                        AND $wpdb->postmeta.meta_value = 1 
                        AND $wpdb->posts.post_type = 'page'
                        ORDER BY $wpdb->posts.post_date DESC
                     ";
$pageposts = $wpdb->get_results($querystr, OBJECT);

试试这个,这应该有用。