1functionget_post_number($previous=false,$same_term=true,$taxonomy='category')2{3global$wpdb;45if((!$post=get_post())||!taxonomy_exists($taxonomy))6returnnull;78$current_post_date=$post->post_date;9$join='';10$where='';1112if($same_term){13$join.=" INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";14$where.=$wpdb->prepare("AND tt.taxonomy = %s",$taxonomy);1516if(!is_object_in_taxonomy($post->post_type,$taxonomy))17returnnull;1819$terms=get_the_terms($post->ID,$taxonomy);20if($terms){21$terms=wp_list_sort($terms,array('term_id'=>'ASC'));22$term=$terms[0];23$where.=" AND tt.term_id = {$term->term_id}";24}25}2627if(is_user_logged_in()){28$user_id=get_current_user_id();2930$post_type_object=get_post_type_object($post->post_type);31if(empty($post_type_object)){32$post_type_cap=$post->post_type;33$read_private_cap='read_private_'.$post_type_cap.'s';34}else{35$read_private_cap=$post_type_object->cap->read_private_posts;36}3738$private_states=get_post_stati(array('private'=>true));39$where.=" AND ( p.post_status = 'publish'";40foreach((array)$private_statesas$state){41if(current_user_can($read_private_cap)){42$where.=$wpdb->prepare(" OR p.post_status = %s",$state);43}else{44$where.=$wpdb->prepare(" OR (p.post_author = %d AND p.post_status = %s)",$user_id,$state);45}46}47$where.=" )";48}else{49$where.=" AND p.post_status = 'publish'";50}5152$op=$previous?'<=':'>=';53$order=$previous?'ASC':'DESC';5455$where=$wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s $where",$current_post_date,$post->post_type);56$sql="SELECT COUNT(*) FROM $wpdb->posts AS p $join$where ORDER BY p.post_date $order";57$number=(int)$wpdb->get_var($sql);5859return$number;60}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/02 08:33