Created
October 31, 2017 08:19
-
-
Save Tenderfeel/49ceeba3ae8588a102574395bd1e9a6f to your computer and use it in GitHub Desktop.
Wordpress get_category_posts_data
This file contains hidden or 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
<?php | |
/* | |
$cat_idで渡したカテゴリーIDに登録されている記事の情報を返す | |
返す情報はSELECTの内容に基づく(記事IDと記事タイトル) | |
参考:https://qiita.com/m-shin/items/cec1b8278448e70be168 | |
get_category_posts_data(get_cat_ID(single_cat_title('', false))); | |
*/ | |
function get_category_posts_data($cat_id) { | |
global $wpdb; | |
return $wpdb->get_results($wpdb->prepare( | |
" | |
SELECT ID, post_title | |
FROM $wpdb->posts | |
WHERE post_status = 'publish' | |
AND $wpdb->posts.post_type = 'post' | |
AND id = ( | |
SELECT object_id | |
FROM $wpdb->term_relationships | |
WHERE term_taxonomy_id = ( | |
SELECT term_taxonomy_id | |
FROM $wpdb->term_taxonomy AS tt | |
INNER JOIN $wpdb->terms AS tm ON tt.term_id = tm.term_id | |
WHERE tm.term_id = %d | |
) | |
) | |
", | |
$cat_id | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment