2015-03-30 64 views
0

我試圖獲得標記爲標記爲特定類別的投資組合的職位列表。對於那個類別,然後我需要限制每個類別的每個結果集,並且只爲每個帖子獲取最新更新的帖子。Wordpress自定義查詢獲取最後編輯的郵件與特定類別

我當前的查詢是這樣的:

SELECT $wpdb->posts.*, $wpdb->terms.slug 
        FROM $wpdb->posts 
        LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) 
        LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
        LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) 
        LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) 
        WHERE $wpdb->posts.post_status = 'publish' 
        AND $wpdb->posts.post_type = 'portfolio' 
        AND $wpdb->posts.post_date < NOW() 
        AND $wpdb->term_taxonomy.taxonomy = 'category' 
        AND $wpdb->terms.slug IN ('".implode("','", $category_array)."') 
        ORDER BY 
         Case When $wpdb->terms.slug = 'residential' Then 1 
          When $wpdb->terms.Slug = 'recreational' Then 1 
          When $wpdb->terms.Slug = 'civic' Then 1 
          When $wpdb->terms.Slug = 'contact' Then 2 
          When $wpdb->terms.Slug = 'blog' Then 3 
          When $wpdb->terms.Slug = 'services' Then 4 
          When $wpdb->terms.Slug = 'team' Then 5 
          Else 6 
         End 

我會使用這樣的(MySQL limit results per category),以獲得爲每個類別設置的限制。

但是,這會返回當前帖子的每個版本。我不確定如何去獲取最新版本的帖子。我知道我如何「能夠」這樣做,但非常懷疑這是最好的方式。有誰知道用這種自定義查詢獲取最新帖子的標準方式嗎?

此外,我不一定致力於自定義查詢。我只是不知道如何使用WP_Query()函數的參數創建此查詢。

謝謝, 詹姆斯

回答

0

所以我結束了使用子查詢。這是我用過的。

$querystr = "SELECT x.* 
         FROM (
          SELECT y.*, 
           CASE 
            WHEN @category != y.slug THEN @rownum := 1 
            ELSE @rownum := @rownum + 1 
           END AS rank, 
           @category := y.slug 
           FROM (
            SELECT $wpdb->posts.*, 
             $wpdb->terms.slug 
            FROM $wpdb->posts 
             LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) 
             LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
             LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) 
             LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) 
            WHERE $wpdb->posts.post_status = 'publish' 
             AND $wpdb->posts.post_type = 'portfolio' 
             AND $wpdb->posts.post_date < NOW() 
             AND $wpdb->terms.slug IN ('".implode("','", $category_array)."') 
             AND $wpdb->posts.ID NOT IN 
              (
               SELECT $wpdb->posts.ID 
                FROM $wpdb->posts 
                LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) 
                LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
                LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) 
                LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) 
                WHERE $wpdb->posts.post_status = 'publish' 
                AND $wpdb->posts.post_type = 'portfolio' 
                AND $wpdb->posts.post_date < NOW() 
                AND $wpdb->terms.slug IN ('".implode("','", $category_array)."') 
                AND $wpdb->posts.post_type = 'revision' 
                GROUP BY $wpdb->posts.ID 
              ) 
            GROUP BY $wpdb->posts.ID 
            ORDER BY $wpdb->terms.slug 
           ) y 
           LEFT JOIN (SELECT @rownum := 0, @category := NULL) r ON (1 = 1) 
         ) x 
         WHERE x.rank <= 10 
         ORDER BY 
          Case When x.slug = 'residential' Then 1 
           When x.Slug = 'recreational' Then 1 
           When x.Slug = 'civic' Then 1 
           When x.Slug = 'contact' Then 2 
           When x.Slug = 'blog' Then 3 
           When x.Slug = 'services' Then 4 
           When x.Slug = 'team' Then 5 
           Else 6 
          End, 
          rand()"; 
相關問題