[Solved] How to change a custom query into a standard loop?


In order to be able to use template tags, you have to do two things after getting your custom select query:

  1. You have to declare the global $post variable.
  2. You have to call the setup_postdata($post) function to populate the variables.

So, my code had to be changed like so:

$results = $wpdb->get_results($wp_query->request, OBJECT);
global $post;
foreach ($results as $post) {
    setup_postdata($post); 
?>
<h4><a href="https://wordpress.stackexchange.com/questions/23088/<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>

etc.

The key to this is on this codex page:

Displaying posts using a custom select query

solved How to change a custom query into a standard loop?