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

カテゴリー中で何番目の記事か

More than 5 years have passed since last update.

JOIN が多くなるのでカテゴリーの slug よりも ID を使った方が良いかもしれませんが・・・。

functions.php
function post_number_in_cat($cat_slug) {
  global $wpdb, $post;
  $number = $wpdb->get_var("
    SELECT COUNT(posts.ID)
    FROM $wpdb->posts posts
    INNER JOIN $wpdb->term_relationships rels ON posts.ID = rels.object_id
    INNER JOIN $wpdb->term_taxonomy tax ON rels.term_taxonomy_id = tax.term_taxonomy_id
    INNER JOIN $wpdb->terms terms ON tax.term_id = terms.term_ID
    WHERE terms.slug = '{$cat_slug}'
    AND posts.post_date <= '{$post->post_date}'
    AND posts.post_status = 'publish'
    AND posts.post_type = 'post'
  ;");
  return $number;
}
shuhei
日々「知らなかったなー」と思ったことを書いていきます。
https://shuheikagawa.com
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