2014-07-09 107 views
0

假設我正在開發一個最小主題,帖子頁single.php僅顯示帖子標題,摘錄和內容。哪個更好更快? Wordpress查詢或SQL查詢

一般

因此,這將是

<?php the_title(); ?> 
<?php the_excerpt(); ?> 
<?php the_content(); ?> 

所以我期待3個查詢...

相反,如果我寫了一個自定義查詢哪裏,我在一個陣列一次只取這3個DATAS ...

<?php arr[] = get_row(select title, excerpt, content from wp_posts where post_id = 1); ?> 
<?php echo arr[0] ?> 
<?php echo arr[1] ?> 
<?php echo arr[2] ?> 

這是更快,這有區別嗎?

PLZ忽略句法如果錯...

+2

您可以通過在for循環上執行一點基準來輕鬆測試哪個更快。 – Prix

回答

2

the_title功能的使用路徑是

the_title> get_the_title> get_post> WP_Post

在結束時,WP_Post get_instance方法具有以下線

$_post = wp_cache_get($post_id, 'posts'); 

if (! $_post) { 
    $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id)); 

    if (! $_post) 
     return false; 

    $_post = sanitize_post($_post, 'raw'); 
    wp_cache_add($_post->ID, $_post, 'posts'); 
} elseif (empty($_post->filter)) { 
    $_post = sanitize_post($_post, 'raw'); 
} 

所以,the_title使用get_row方法。它是SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1而你的是select title, excerpt, content from wp_posts where post_id = 1。也許你可以將LIMIT 1添加到你的sql代碼中。

wp_cache_get是這些所有東西的差異。緩存將改變萬物,如果它運作良好。