2011-02-24 2 views
0

쿼리에서 반환 된 게시물 수를 계산하고 반환 된 게시물을 구문 분석하는 가장 효율적인 방법은 무엇입니까 (시간별)?

나는 현재이 코드가 :

PART 1

/* $query = { 
'numberposts' = '15', 
'queryvar1' = '…', 
'queryvar2' = '…'; 
} 
*/ 
$lastposts = get_posts($query); // This uses the original query, and will only return 15 results 

$print['returnedcount'] = count($lastposts); // Uses some resources (+ acceptable time) 

$query['numberposts'] = "-1"; // Get total results from query 
$print['totalposts'] = count(get_posts($query)); // Uses lots of resources (+ lots of time) 

나는이 두 번째 get_posts($query)이 나를 호가 표시 다른 데이터에 대한 아무 소용이 없습니다을, 어떻게 최대 것을 속도를 높일 수 있습니다? 난 단지 numberposts - 값을 제외하고 쿼리 (에 의해 반환 게시물의 총 수를 계산해야하는

2 부 :.

후 데이터 (ID, 날짜를 얻기 위해 나중에 사용됩니다 -object $lastposts ., 제목, 댓글 수, 썸네일 및 작성자 ID)

이 datas

$print에 inputed하는이 맘에 -array :
foreach ($lastposts as $post){ 
    // ID 
    $print['id'][] = (string)$post->ID; 
    // Date 
    $print['date'][] = (string)$post->post_date; 
    // Title 
    $print['title'][] = (string)$post->post_title; 
    // Comment count 
    $print['comments'][] = (string)$post->comment_count; 
    // Images 
    $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); 
    $print['image'][] = (string)$image[0]; 
    $imageRetina = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large'); 
    $print['imageretina'][] = (string)$imageRetina[0]; 
    // Author 
    $print['author'][] = (string)$post->post_author; 
} 

이 더인가 시간을 효율적으로 사용하는 방법? 나는 이미지 - 행동이 꽤 오랜 시간이 걸리는 것을 알아 챘다.

고마워요!

답변

2

파트 1의 경우 get_posts을 사용하는 대신 새로운 WP_Query 개체를 만들 수 있습니다. 이것은 질문의 코드보다 더 효율적이어야하지만 그 차이는 무시할 만합니다.

$query = new WP_Query(); 
$query->query(array(
    'posts_per_page' => 15 
)); 

$print['returnedcount'] = $query->post_count; 
$print['totalposts'] = $query->found_posts; 
+0

감사합니다, 나는 시도 할 것입니다 : – Emil

+0

이것은 끝내 었습니다. 고마워요! :) – Emil

-1

당신은 쿼리가 실행되는 최초의 시간을 필요로 배열을 채울 수 - get_posts() 불필요한 실행 할 것이라고.

그래서, 템플릿 파일에 당신은

while(have_posts()): the_post(); 
    // do your normal output here e.g. the_content(); etc. 

    $print['title'][] = get_the_title(); 
    // any more data you want to collect. 

endwhile; 

당신은 전체 데이터 수집 비트 functions.php로 이동 할 수 있습니다. 당신도 캐싱을 사용하고 있습니까? 그게 도움이 될거야. (예 : http://wordpress.org/extend/plugins/hyper-cache/)

+0

AJAX 요청을 통해 실행되도록 플러그인을 개발 중이므로 루프를 사용합니다. 불행히도 옵션이 아님 :) – Emil

+0

세션 변수를 사용하여 루프 중에 얻은 정보를 저장 한 다음 나중에 아약스 요청이 들어올 때 해당 정보를 사용할 수 있습니까? – JasonC