Not Showing Results From DB Query

I use multisite and am trying to query my data base to get content from one site to display on another. Autoblog doesn't really suit my purposes for what I'm trying to do, so I tried adding something like

<?php
/*
Template Name: Qbased
*/
?>

<?php get_header(); ?>

<div id="content" class="narrowcolumn">

<?php

global $wpdb;

$querystr ="
    SELECT *
    FROM $wpdb->wp_3_posts
    WHERE post_type='post'
";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

?>
 <?php if ($pageposts): ?>
  <?php global $post; ?>
  <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
      <div class="entry">
         <?php the_content('Read the rest of this entry »'); ?>asdf
      </div>

    </div>
  <?php endforeach; ?>

  <?php else : ?>
    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>

    <?php include (TEMPLATEPATH . "/searchform.php"); ?>
 <?php endif; ?>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

This is a modification of what I found here.

All I'm getting is the content from the else statement. Can someone tell me what I'm doing wrong please.

  • Dimitris

    Hey there Justin,

    hope you're doing good and thanks for reaching us! :slight_smile:

    Could you please try next snippet instead?

    <?php
    /*
    Template Name: Qbased
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content" class="narrowcolumn">
    
      <?php
    
       $querystr = "
          SELECT *
          FROM wp_3_posts
          WHERE wp_3_posts.post_type = 'post'
       ";
    
       $pageposts = $wpdb->get_results($querystr, OBJECT);
    
      ?>
       <?php if ($pageposts): ?>
        <?php global $post; ?>
        <?php foreach ($pageposts as $post): ?>
          <?php setup_postdata($post); ?>
    
          <div class="post" id="post-<?php the_ID(); ?>">
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
            <?php the_title(); ?></a></h2>
            <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
            <div class="entry">
               <?php the_content('Read the rest of this entry »'); ?>
            </div>
    
            <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
            <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
          </div>
        <?php endforeach; ?>
    
        <?php else : ?>
          <h2 class="center">Not Found</h2>
          <p class="center">Sorry, but you are looking for something that isn't here.</p>
          <?php include (TEMPLATEPATH . "/searchform.php"); ?>
       <?php endif; ?>
    
    </div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Let me know how that goes!
    Warm regards,
    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.