How can I save the HTML output of a WP_Query function as a variable?

The WP_Query class does not generate HTML markup. WP_Query will query the WordPress content, and provide some data back. So using WP_Query and the sprintf() PHP function, the HTML markup can be generated (untested):

$args = array(
    ...
);

$query = new WP_Query( $args );
$html="";

while ( $query->have_posts() ) {
    $query->the_post();

    $saved_html .= sprintf( '<a href="%s">%s</a>', esc_url( get_the_permalink() ), esc_html( get_the_title() ) );
}

echo $saved_html;

You could also use PHP’s output buffering if you prefer, but sprintf() is the quickest/simplest implementation.

techhipbettruvabetnorabahisbahis forumutaraftarium24edueduedueduseduedueduseduedusedus