How to get Custom Post Type (CPT) Archive Page’s Posts Featured Images Preload in header.php

You can access the current query on a custom post type archive page with global $wp_query;. By using the global variable which holds the current query, you don’t need to do an additional query.

To keep your header.php file cleaner, you could use actions and callback functions placed in your functions.php file to put the preload links in the head tag.

First action for checking, if the preload links should be added or not.

add_action('template_redirect', 'wpse_425594_post_type_archive_preload_links');
function wpse_425594_post_type_archive_preload_links() {
    if ( is_post_type_archive('my-post-type') ) {
        add_action( 'wp_head', 'wpse_425594_current_query_post_images_preload' );
    }
}

The second callback for rendering the preload links using the posts from the current query.

function wpse_425594_current_query_post_images_preload() {
    global $wp_query;

    $preload_links = array_filter( array_map(
        function( $post ) {
            $url = get_the_post_thumbnail_url( $post );

            return $url
                ? sprintf( '<link rel="preload" as="image" href="%s" />', esc_url( $url ) ) // update as needed
                : '';
        },
        $wp_query->posts
    ) );

    echo implode( "\n", $preload_links );
}

techhipbettruvabetnorabahisbahis forumuedusedueduedueduseduedusedusedusedus