Created
February 1, 2016 09:23
-
-
Save ki6ool/c1940b60432a3df3b27f to your computer and use it in GitHub Desktop.
[Wodpress]各投稿タイプ最新1件を取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @link http://betterwp.net/latest-posts-from-each-post-type/ | |
*/ | |
global $wpdb; | |
$query = <<<EOT | |
SELECT * FROM | |
( | |
SELECT post_type, max(post_date) AS tmp FROM {$wpdb->posts} | |
WHERE post_status = 'publish' | |
AND post_type NOT IN ('post', 'page') | |
GROUP BY post_type | |
) AS post | |
INNER JOIN {$wpdb->posts} AS ij ON ij.post_type = post.post_type | |
AND ij.post_date = post.tmp | |
EOT; | |
$posts = $wpdb->get_results($wpdb->prepare($query, null)); | |
foreach ($posts as $post) { | |
setup_postdata($post); | |
// | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment