Wordpress - Fatal error: Call to a member function get_var() on a non-object $wpdb

人走茶凉 提交于 2019-12-01 21:16:30

globalize $wpdb before this

global $wpdb

$post_id = $wpdb->get_var( $wpdb->prepare("SELECT id FROM $wpdb->posts WHERE post_password = %s", $post_password) ); $q = new WP_Query( 'p=$post_id' );

Also it is a best practise to use lowercase table/column names

and then redirect like so

<?php if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); ?>

wp_redirect( the_permalink() );

<?php endif;?>

Also use a http status code as the second parameter to wp_redirect() This might be helpful HTTP STATUS CODES

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!