Another Addition to my WordPress as CMS series is how you can display posts from another WordPress installation but residing in the same database.
I know you would say it can be done by custom SQL queries and then spitting out content in the manner you would like but there is a better technique of doing this with which you can use the regular inside loop functions and the credit goes to setup_postdata() function.
Display posts from another WordPress installation in the same database
Lets take a look how I did it :
<?php $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'tag' AND wpostmeta.meta_value = 'email' AND wposts.post_status = 'publish' AND wposts.post_type = 'post' AND wposts.post_date < NOW() ORDER BY wposts.post_date DESC "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?> <?php if ($pageposts): ?> <?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(); ?>"> <?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; ?>
The above code has taken from the Codex as an example, I just modified the query and the in loop contents as per need but for reference the Codex example is a good example.
We executed a query to fetch results from database using $wpdb class and store the returned object in $pageposts and then with the help of foreach loop we can iterate it.
Inside the foreach loop we used the setup_postdata() function which will populate all the fields and then we can use the code to display out the content as we do inside a WordPress loop.
The above code looks similar to the regular WordPress loop with the difference that instead of using the WordPress loop, we had to fetch the data by a query ourself and iterate through it.
Display posts from another WordPress installation in a different database
If you are trying to fetch posts from another WordPress installation, then either you can do it by custom PHP-MySQL query interacting with the database and then iterating it like shown above but the disadvantage associated with this method is that this query will not be cached because we are not using the $wpdb class or another method will be to create a new object of $wpdb class with the connection details to the different database and then using the same procedure like above.
What do you think? Do you have any other or may be an easier way of doing this? Let me know through the comments or if you have any question, feel free to ask.