Reputation: 2148
Suppose I am developing a minimal theme, the the post page single.php
displays only the post title, the excerpt and the content.
So in general that would be
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php the_content(); ?>
So I am expecting 3 queries...
Instead if I write a custom query where I am just fetching these 3 datas in an array at once...
<?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] ?>
Which is faster or does it make a difference ?
Plz ignore syntax if wrong...
Upvotes: 0
Views: 298
Reputation: 1942
the_title
function usage path is
the_title > get_the_title > get_post > WP_Post
At the end, WP_Post get_instance method has following lines
$_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' );
}
So, the_title use get_row method. And it's is SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1
and yours is select title, excerpt, content from wp_posts where post_id = 1
. maybe you can add LIMIT 1
to your sql code.
But wp_cache_get
is the difference of these all things. Cache will change everythings if it works good.
Upvotes: 2