무작정 워드프레스 코어를 디버깅하려다간 낭패를 보기 십상이다. 생소한 함수며 전역 변수, 함수 간 호출 관계, 용도 등 파악해야 할 사항들이 너무 많기 때문이다.
천리길도 한 걸음이란 말처럼 주요 함수들부터 차근차근 파악해야 한다. 친절하게도 대부분 코어 파일은 인라인 설명서가 있다. 해당 함수 바로 앞에 블록 주석 형식으로 상세한 설명이 들어간다.
/**
* Retrieve list of latest posts or posts matching criteria.
*
* The defaults are as follows:
* ‘numberposts’ – Default is 5. Total number of posts to retrieve.
* ‘offset’ – Default is 0. See {@link WP_Query::query()} for more.
* ‘category’ – What category to pull the posts from.
* ‘orderby’ – Default is ‘post_date’. How to order the posts.
* ‘order’ – Default is ‘DESC’. The order to retrieve the posts.
* ‘include’ – See {@link WP_Query::query()} for more.
* ‘exclude’ – See {@link WP_Query::query()} for more.
* ‘meta_key’ – See {@link WP_Query::query()} for more.
* ‘meta_value’ – See {@link WP_Query::query()} for more.
* ‘post_type’ – Default is ‘post’. Can be ‘page’, or ‘attachment’ to name a few.
* ‘post_parent’ – The parent of the post or post type.
* ‘post_status’ – Default is ‘publish’. Post status to retrieve.
*
* @since 1.2.0
* @uses $wpdb
* @uses WP_Query::query() See for more default arguments and information.
*
* @param array $args Optional. Overrides defaults.
* @return array List of posts.
*/
get_posts()의 인라인 설명서이다. 메소드의 요약 설명과 상세 설명, 버전 정보, 전역 변수 사용 정보, 상세 설명이 담긴 codex 페이지, 파라메터 및 리턴 값 정보 등이 등록되어 있다. 좀 더 자세한 정보는 Codex 사이트에 가면 예제까지 볼 수 있다.
자세히 보면
- 이 버전은 1.2.0
- $wpdb와 WP_Query::query()를 전역으로 호출해서 사용
- 자세한 코텍스 사이트는 http://codex.wordpress.org/Template_Tags/get_posts
- 파라메터는 어레이를 옵셔널하게
- 리턴값은 포스트에 대한 어레이를 반환
인라인 설명서만으로도 코어 소스를 보는 데 많은 도움이 된다. get_posts() 메소드를 열어 인라인 설명서를 바탕으로 실제 코드를 분석하면 많은 도움이 될 것이다.
function get_posts($args = null) {
$defaults = array(
‘numberposts’ => 5, ‘offset’ => 0,
‘category’ => 0, ‘orderby’ => ‘post_date’,
‘order’ => ‘DESC’, ‘include’ => array(),
‘exclude’ => array(), ‘meta_key’ => ”,
‘meta_value’ =>”, ‘post_type’ => ‘post’,
‘suppress_filters’ => true
);
$r = wp_parse_args( $args, $defaults );
if ( empty( $r[‘post_status’] ) )
$r[‘post_status’] = ( ‘attachment’ == $r[‘post_type’] ) ? ‘inherit’ : ‘publish’;
if ( ! empty($r[‘numberposts’]) && empty($r[‘posts_per_page’]) )
$r[‘posts_per_page’] = $r[‘numberposts’];
if ( ! empty($r[‘category’]) )
$r[‘cat’] = $r[‘category’];
if ( ! empty($r[‘include’]) ) {
$incposts = wp_parse_id_list( $r[‘include’] );
$r[‘posts_per_page’] = count($incposts); // only the number of posts included
$r[‘post__in’] = $incposts;
} elseif ( ! empty($r[‘exclude’]) )
$r[‘post__not_in’] = wp_parse_id_list( $r[‘exclude’] );
$r[‘ignore_sticky_posts’] = true;
$r[‘no_found_rows’] = true;
$get_posts = new WP_Query;
return $get_posts->query($r);
}