Help us understand the problem. What is going on with this article?

[WordPress]各カテゴリー最新1件の投稿を取得する

More than 1 year has passed since last update.

以前クライアントから表題のような仕様で
(正確には、さらにランダムかつ計50件まで)
依頼を受けた。

結局この件は、だいたいよくあるパターンだが
運用では数種類カテゴリーの記事しか更新されないので
ほとんど旧い記事が表示されるんやでっていうことで普通の仕様になった。

とはいえ、今後もこのようなわけわからん依頼がくるやもしれんので
とりあえず残すことにする。

$query = <<<SQL
SELECT WP.ID, WTT.term_id FROM wp_term_taxonomy AS WTT
    INNER JOIN wp_term_relationships AS WTR ON WTR.term_taxonomy_id = WTT.term_taxonomy_id
    INNER JOIN (SELECT ID, post_type, post_status FROM wp_posts ORDER BY post_date DESC) AS WP ON WP.ID = WTR.object_id
WHERE
    WTT.taxonomy = %s
    AND WTT.count > %d
    AND WP.post_type = %s
    AND WP.post_status = %s
GROUP BY WTT.term_taxonomy_id
ORDER BY RAND()
LIMIT 50
SQL;

global $wpdb;
$ret = $wpdb->get_results($wpdb->prepare($query, ['category', 0, 'post', 'publish']), ARRAY_A);

これで投稿IDとタームIDが取れるので
あとはよろしくやってほしい。

ちなみに投稿タイプverは下記
[WordPress]各投稿タイプ最新1件を取得する

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした