Written by Anonymous
function get_related_wp_query_args(){
global $post;
if (!$post) {
$post = get_random_posts(1);
}
$args = array();
$set_args = array(
'post__not_in' => array($post->ID),
...
);
if ( is_related_association_type_category() ) {
$set_args['category__in'] = get_cate_ids($post->ID);
if (!empty($set_args['category__in'])) $args = $set_args;
} else {
$set_args['tag__in'] = get_tag_ids($post->ID);
if (!empty($set_args['tag__in'])) $args = $set_args;
}
return apply_filters('get_related_wp_query_args', $args);
}
//投稿のカテゴリー情報を取得
function get_cate_ids($post_id) {
$categories = get_the_category($post_id);
$category_IDs = array();
$cat_count = 0;
...
return $category_IDs;
}
//投稿のタグ情報を取得
function get_tag_ids($post_id) {
//get_cate_ids関数と同様
...
return $tag_IDs;
}
//$argsが空のとき
add_filter('get_related_wp_query_args', 'custom_related_args');
function custom_related_args($args) {
if (empty($args)) {
...
if ( is_related_association_type_category() ) {
$set_args['tag__in'] = get_tag_ids($post->ID);
...
} else {
$set_args['category__in'] = get_cate_ids($post->ID);
...
}
...
}
return $args;
}