Written by Anonymous

  1. function get_related_wp_query_args(){
  2. global $post;
  3. if (!$post) {
  4. $post = get_random_posts(1);
  5. }
  6. $args = array();
  7. $set_args = array(
  8. 'post__not_in' => array($post->ID),
  9. ...
  10. );
  11.  
  12. if ( is_related_association_type_category() ) {
  13. $set_args['category__in'] = get_cate_ids($post->ID);
  14. if (!empty($set_args['category__in'])) $args = $set_args;
  15. } else {
  16. $set_args['tag__in'] = get_tag_ids($post->ID);
  17. if (!empty($set_args['tag__in'])) $args = $set_args;
  18. }
  19.  
  20. return apply_filters('get_related_wp_query_args', $args);
  21. }
  22.  
  23. //投稿のカテゴリー情報を取得
  24. function get_cate_ids($post_id) {
  25. $categories = get_the_category($post_id);
  26. $category_IDs = array();
  27. $cat_count = 0;
  28. ...
  29. return $category_IDs;
  30. }
  31.  
  32. //投稿のタグ情報を取得
  33. function get_tag_ids($post_id) {
  34. //get_cate_ids関数と同様
  35. ...
  36. return $tag_IDs;
  37. }
  38.  
  39. //$argsが空のとき
  40. add_filter('get_related_wp_query_args', 'custom_related_args');
  41. function custom_related_args($args) {
  42. if (empty($args)) {
  43. ...
  44. if ( is_related_association_type_category() ) {
  45. $set_args['tag__in'] = get_tag_ids($post->ID);
  46. ...
  47. } else {
  48. $set_args['category__in'] = get_cate_ids($post->ID);
  49. ...
  50. }
  51. ...
  52. }
  53.  
  54. return $args;
  55. }
Notepad
Select All