Last active
March 20, 2018 16:15
-
-
Save Go-Noji/c07b3c6ba0c91b60960d3016257924cf to your computer and use it in GitHub Desktop.
WordpressでカテゴリーAのBかつC該当しており、さらにカテゴリーDのEまたはFまたはG...に該当する投稿を取得する ref: https://qiita.com/Go-Noji/items/7604cc3b0cf68147af5e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//nonceの検証(失敗したらスクリプトが停止) | |
check_ajax_referer('ajaxNonce', 'nonce'); | |
//グローバルからwpdbクラスを呼び出し | |
global $wpdb; | |
//検索条件である地域カテゴリー | |
$categories = isset($_POST['categories']) ? (array)$_POST['categories'] : array(); | |
//追加WHERE文の作成 | |
$wheres[] = array; | |
foreach ((array)$categories as $category) | |
{ | |
//そもそもint型にできない or 整数値ではなさげな値を弾く | |
if ( ! is_scalar($category) || ! ctype_digit((string)$category)) | |
{ | |
continue; | |
} | |
$wheres[] = " wp_term_taxonomy.term_taxonomy_id = ".(int)esc_sql($category)." "; | |
} | |
$where = $wheres ? '('.implode('OR', ).$wheres.')' : ''; | |
//LIMIT, OFFSET | |
$limit = isset($_POST['limit']) && is_scalar($_POST['limit']) ? (int)$_POST['limit'] : 30; | |
$offset = isset($_POST['offset']) && is_scalar($_POST['offset']) ? (int)$_POST['offset'] : 0; | |
//「女性向け(スラッグ名=forWormen)」「スイーツ推し(スラッグ名=sweets)」のカテゴリーを持つ投稿のみ取得 | |
//ここで取得する列の最大値は実際に返す列より一つ多く取得する | |
//なぜなら、次の記事が存在するか判定するため | |
if ($whereCount) | |
{ | |
$query = $wpdb->prepare(" | |
SELECT DISTINCT wp_posts.* | |
FROM `wp_term_taxonomy` | |
LEFT JOIN `wp_term_relationships` ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id | |
LEFT JOIN `wp_posts` ON wp_term_relationships.object_id = wp_posts.ID | |
WHERE {$where} | |
AND wp_term_taxonomy.taxonomy = 'area' | |
AND ID IN(SELECT DISTINCT wp_posts.ID | |
FROM `wp_terms` | |
LEFT JOIN `wp_term_taxonomy` ON wp_terms.term_id = wp_term_taxonomy.term_id | |
LEFT JOIN `wp_term_relationships` ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id | |
LEFT JOIN `wp_posts` ON wp_term_relationships.object_id = wp_posts.ID | |
WHERE (wp_terms.slug = 'forWomen' OR wp_terms.slug = 'sweets') | |
AND wp_posts.post_type = 'post' | |
AND wp_posts.post_status = 'publish' | |
AND wp_term_taxonomy.taxonomy = 'category' | |
GROUP BY wp_posts.ID | |
HAVING COUNT(wp_posts.ID) = 2) | |
LIMIT %d, %d | |
", $offset, $limit + 1); | |
} | |
else | |
{ | |
$query = $wpdb->prepare(" | |
SELECT wp_posts.* | |
FROM `wp_terms` | |
LEFT JOIN `wp_term_taxonomy` ON wp_terms.term_id = wp_term_taxonomy.term_id | |
LEFT JOIN `wp_term_relationships` ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id | |
LEFT JOIN `wp_posts` ON wp_term_relationships.object_id = wp_posts.ID | |
WHERE (wp_terms.slug = 'forWomen' OR wp_terms.slug = 'sweets') | |
AND wp_posts.post_type = 'post' | |
AND wp_posts.post_status = 'publish' | |
AND wp_term_taxonomy.taxonomy = 'category' | |
GROUP BY wp_posts.ID | |
HAVING COUNT(wp_posts.ID) = 2 | |
LIMIT %d, %d | |
", $offset, $limit + 1); | |
} | |
//データ取得 | |
$data = $wpdb->get_results($query, ARRAY_A); | |
//次の記事が存在するかを情報に加える | |
$isNext = isset($data[$limit]) && $data[$limit] ? TRUE : FALSE; | |
//上記存在判定のために余分に取っておいた列を詰める | |
if (isset($data[$limit]) && $data[$limit]) | |
{ | |
array_pop($data); | |
} | |
//jsonに加工してデータを吐き出す | |
print json_encode(compact('data', 'isNext'), TRUE); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT wp_posts.* | |
FROM `wp_terms` | |
LEFT JOIN `wp_term_taxonomy` ON wp_terms.term_id = wp_term_taxonomy.term_id | |
LEFT JOIN `wp_term_relationships` ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id | |
LEFT JOIN `wp_posts` ON wp_term_relationships.object_id = wp_posts.ID | |
WHERE (wp_terms.slug = 'forWomen' OR wp_terms.slug = 'sweets') | |
AND wp_posts.post_type = 'post' | |
AND wp_posts.post_status = 'publish' | |
AND wp_term_taxonomy.taxonomy = 'category' | |
GROUP BY wp_posts.ID | |
HAVING COUNT(wp_posts.ID) = 2 | |
LIMIT 0, 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT DISTINCT wp_posts.* | |
FROM `wp_term_taxonomy` | |
LEFT JOIN `wp_term_relationships` ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id | |
LEFT JOIN `wp_posts` ON wp_term_relationships.object_id = wp_posts.ID | |
WHERE (wp_term_taxonomy.term_taxonomy_id = 1 OR wp_term_taxonomy.term_taxonomy_id = 2 OR wp_term_taxonomy.term_taxonomy_id = 3) | |
AND wp_term_taxonomy.taxonomy = 'area' | |
AND ID IN(SELECT DISTINCT wp_posts.ID | |
FROM `wp_terms` | |
LEFT JOIN `wp_term_taxonomy` ON wp_terms.term_id = wp_term_taxonomy.term_id | |
LEFT JOIN `wp_term_relationships` ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id | |
LEFT JOIN `wp_posts` ON wp_term_relationships.object_id = wp_posts.ID | |
WHERE (wp_terms.slug = 'forWomen' OR wp_terms.slug = 'sweets') | |
AND wp_posts.post_type = 'post' | |
AND wp_posts.post_status = 'publish' | |
AND wp_term_taxonomy.taxonomy = 'category' | |
AND | |
GROUP BY wp_posts.ID | |
HAVING COUNT(wp_posts.ID) = 2) | |
LIMIT 0, 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment