admin管理员组文章数量:1289632
I have several WordPress sites that I manage where I will need to get post data from one site and display it on another. Ideally I would like to connect to the second site's db and pull the post information using WordPress's functions. For example, when connecting to another site I would like to use the get_post_types() function to pull the post type information from the second site. In the code below I attempted to set the $wpdb variable to the second site's connection but get_post_types() is still returning information from the main site. When I dump the $wpdb after connecting to the second site it looks like it is connected to the second site but WordPress functions relating to the second site don't work.
global $wpdb;
$wpdb_backup = $wpdb;
//dump main site post types
var_dump(get_post_types());
$db = $bos['afs']['db_name'];
$user = $bos['afs']['db_user'];
$password = $bos['afs']['db_pass'];
$host = $bos['afs']['db_host'];
$wpdb = new wpdb( $user, $password, $db, $host );
wp_set_wpdb_vars();
//dump query (works. results from second site)
var_dump($wpdb->get_results('Query Here'))
//dump second sites post types (doesn't work. results from main site)
var_dump(get_post_types());
//set to original
$wpdb = $wpdb_backup;
Is it even possible to get this functionality working?
本文标签: How can I connect to a second database and still be able to use wp functions like getposttypes()
版权声明:本文标题:How can I connect to a second database and still be able to use wp functions like get_post_types()? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741477779a2380986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论