External DB Connection, retrieving Metadata
-
Hi all
I’m currently building a company intranet and for the staff directory aspect of it, I want to pull in the staff directory that already exists on the company’s public website. It will be displayed differently, but that’s a simple matter of including/excluding certain fields that I don’t want public for the public website etc.
The databases exist on the same server and all of that so connecting is not a problem. I’ve actually managed to successfull query the database of the public website on the intranet using the following code in a template file:
<?php $wpdb2 = new wpdb('username', 'password', 'dbname', 'hostname'); $querystr = "SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) LEFT JOIN wp_postmeta wphptbl ON wp_posts.ID = wphptbl.post_id and wphptbl.meta_key like '_wplp_%' AND ((wp_posts.post_type = 'post' AND wphptbl.meta_key not like '_wplp_%' ) OR (wp_posts.post_type = 'page' AND wphptbl.meta_key <> '_wplp_page_flags' AND wphptbl.meta_key not like '_wplp_%' )) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (142) ) AND wp_posts.post_type = 'staff' AND (wp_postmeta.meta_key = 'surname' ) AND wphptbl.post_id IS NULL GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value ASC "; $smm = $wpdb->get_results($querystr, OBJECT); ?> <?php if ($smm): ?> <?php global $post; ?> <?php foreach ($smm as $post): ?> <?php setup_postdata($post); ?> <div class="post" id="post-<?php the_ID(); ?>" style="margin:1em 0;border-bottom:1px dotted #CCC;padding-bottom:1em;"> <strong><?php the_title(); ?></strong> <div class="clearfix"></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; ?>
This works, however, if I try to retrieve anything other than the title of the post, I can’t. For example, I’m unable to retrieve the post thumbnail or display any of the meta data associated with the post, e.g.
<?php echo get_post_meta($post->ID, “first_name”, TRUE); ?> <?php echo get_post_meta($post->ID, “surname”, TRUE); ?>
I don’t really know SQL and so the query was obtained by “reverse engineering” it; I created a page template in the public website where the data is being drawn from, running WP Query to get the results I wanted and then echoing the query in SQL terms.
As said, the SQL query is basically correct because I’m pulling in the correct data; I’m just not able to display other data that I would like.
Any pointers in the right direction would be greatly appreciated.
- The topic ‘External DB Connection, retrieving Metadata’ is closed to new replies.