使用wp_get_recent_posts在另一台服务器上检索不同的数据库


To use wp_get_recent_posts to retrieve different database at another server

我正在从服务器A访问服务器B上的另一个数据库。两者都是Wordpress服务器。当我想使用wp_get_recent_posts和其他wpGET函数时,它不起作用。我的密码有什么问题吗?

$mydb = new $wpdb( 'username', 'password', 'database', 'stackoverflow.com' );    
$args = array('post_type' => 'product', 
              'post_status' => 'publish', 
              'posts_per_page' => 6, 
              'product_cat' => 'book',);
$recent_posts = $mydb->wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
?>
    <div class="col-xs-6 col-sm-2 col-md-2 col-lg-2 marginbottom-20">
    <?php 
        $thumbnail = get_post_meta($recent["ID"], 'community_thumb', true); 
        echo '<center><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" ><img class="img-responsive thumbnail-border community-books-thumb" src="' .$thumbnail. '"></a></center>'; 
    ?>
    <?php
        echo '<p class="paddingtop-10 fontsize-13"><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' .$recent["post_title"].'</a>';
    ?>
    </div>

查看文档,wp_get_recent_posts似乎只是一个辅助函数。尝试将对象调用从您的代码中删除为:

$recent_posts = wp_get_recent_posts( $args );

顺便说一下,您应该考虑使用WP_Query类。