Last active
October 31, 2017 10:18
-
-
Save Tenderfeel/d12601e903c5c5cf90ed30cc0504ef8b to your computer and use it in GitHub Desktop.
Wordpressでカテゴリーに所属する記事に設定されている指定したカスタムフィールドの値をリスト形式で得る
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
<?php | |
/* | |
カテゴリーに所属する記事に設定されている指定したカスタムフィールドの値をリスト形式で得る | |
*/ | |
function get_category_posts_customfield_value($cat_id, $meta_key) { | |
global $wpdb; | |
$limit = get_option('posts_per_page'); | |
$offset = get_query_var('paged') * $limit; | |
$results = $wpdb->get_col($wpdb->prepare( | |
" | |
SELECT meta_value | |
FROM $wpdb->posts | |
INNER JOIN $wpdb->postmeta AS m1 | |
ON (m1.post_id = $wpdb->posts.ID) | |
AND m1.meta_key = %s | |
WHERE post_status = 'publish' | |
AND $wpdb->posts.post_type = 'post' | |
AND id IN ( | |
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 | |
) | |
) | |
ORDER BY post_date DESC | |
LIMIT $limit OFFSET $offset | |
", | |
$meta_key, | |
$cat_id | |
)); | |
return $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment