get_the_foo() in the loop – does it perform another query?

I’m unclear on the get_the_foo() style of functions, for example get_the_permalink().

My main question is: when this is performed in The Loop, is another SQL query executed (besides the original WP_Query())?

But: if performed by itself, e.g. get_the_permalink($someId), a query is executed, correct?

Solutions Collecting From Web of "get_the_foo() in the loop – does it perform another query?"

Query results for posts, terms, and metadata get placed in cache. If a function is called that accesses data from previously queried objects within the same request (or you’re using a persistent cache mechanism that maintains data across requests), it will not trigger another query and will instead fetch the data from cache.

It seems like yes it depends whether it does create a new query or not. As Milo has pointed out in his answer, WordPress using a caching system. The function stack looks like this:

get_the_permlink() calls get_permalink() calls get_post() which when given a post ID does this:

$_post = WP_Post::get_instance( $post ); which looks like:

 * Retrieve WP_Post instance.
 * @static
 * @access public
 * @param int $post_id Post ID.
 * @return WP_Post|bool Post object, false otherwise.
public static function get_instance( $post_id ) {
        global $wpdb;
        $post_id = (int) $post_id;
        if ( ! $post_id )
                return false;
        $_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' );
        return new WP_Post( $_post );

In this get_instance() function it checks whether it can find the $post_id in the posts cache, if it does then it does not need to actually query the database. I’m not sure if this counts for secondary queries or main queries though.

Sometimes you can get around some of the get_{$field}() functions by accessing the object itself but I don’t think you’ll ever see a huge performance difference by using them. MYSQL is pretty fast and powerful so I wouldn’t consider them a bad thing or something to stay away from.