Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to add the most recent post from another Wordpress database (on the same server in all actuality), but am getting the following error:

Fatal error: Call to undefined method wpdb::query_posts() in /var/www/site/wp-content/themes/custom/footer.php on line 68

Here is the code:

<!-- .entry-content -->
<?php
$originaldb = new wpdb('username', 'password', 'db', 'localhost');
$originaldb->query_posts( 'posts_per_page=1' );  
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 

<header class="entry-header">
        <div class="entry-meta">
            <?php echo '<span class="entry-day">'.get_the_date('j').'</span><br><span class="entry-month">'.get_the_date('M').'</span>'; ?>
        </div>
        <div class="title-box">
            <h2 class="blog-title"><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h2>
            <?php echo '<a href="'.get_author_posts_url( get_the_author_meta( 'ID' ) ).'">' . the_author() . '</a>'; ?>
        </div>
        <div class="clear"></div>
    </header>
    <div class="entry-content">
        <?php the_excerpt(); ?>
    </div>

<?php endwhile; else: ?>  
Testing has failed
<?php endif; ?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

The Wordpress wpdb class is a little different than the normal way of querying in that you run "vanilla" database queries.

You can see documentation on the wpdb class on the Wordpress docs: http://codex.wordpress.org/Class_Reference/wpdb#Run_Any_Query_on_the_Database

Also, in regard to your code, you should be able to change the line here:

$originaldb->query_posts( 'posts_per_page=1' );

To something like this:

$results = $originaldb->get_results( "SELECT * FROM $wpdb->posts LIMIT 1" );

This will provide you with an array of posts that you can then process with a loop:

if($results):
  foreach($results as $post): setup_postdata($post); 
    // do stuff with normal wp template tags
  endforeach;
else: 
  // no posts 
endif;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.