Use wp_query after changing database via new wpdb
我正在尝试使用 wordpress 从其他数据库访问自定义帖子。为此,我更改了当前的
1 2 | $wpdb = new wpdb( $user, $pass, $db, $host ); $wpdb->show_errors(); |
这不会显示任何错误,但是当我尝试使用
1 2 | $args = array('post_type'=>'produtos'); $newloop = new WP_Query($args); |
我收到以下错误:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM WHERE 1=1 AND .post_type = 'produtos' AND (.post_status = 'publish' OR .' at line 1]
SELECT SQL_CALC_FOUND_ROWS .ID FROM WHERE 1=1 AND .post_type = 'produtos' AND (.post_status = 'publish' OR .post_author = 1 AND .post_status = 'private') ORDER BY .post_date DESC LIMIT 0, 10
如果我使用
1 2 3 4 5 6 7 8 | $wpdb = new wpdb( $user, $pass, $db, $host ); $rows = $wpdb->get_results("SELECT * FROM wp_posts where post_type='produtos' AND post_status='publish'"); foreach ($rows as $produto) { $id = $produto->ID; $title = $produto->post_title; $valor = $wpdb->get_var("SELECT meta_value FROM wp_postmeta WHERE meta_key = 'preco' AND post_id = $id"); $url_id = $wpdb->get_var("SELECT meta_value from wp_postmeta where post_id = $id AND meta_key='_thumbnail_id'"); } |
我正在寻找一个优雅的解决方案来解决这个问题。
1 | $wpdb = new wpdb( $user, $pass, $db, $host ); |
创建wpdb对象后,需要设置表名,通过调用
1 |
如果你检查你的 sql 错误。表名是空的。默认表前缀是
解决方案:
1 2 3 4 |