[s_ha_dum在http://wordpress.stackexchange.com上的解决方案]
我正在尝试根据在帖子设置中输入的密码将用户定向到某个帖子.我有几乎工作的代码:
页面上的表格:
<form method="post" action="">
<input type="password" name="passwordfield">
<input type="hidden" name="homepagepassword" value="1">
<input type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
functions.php中的代码
function doPasswordStuff(){
if(isset($_POST['homepagepassword'])){
$post_password = $_POST['passwordfield'];
$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' );
if($q->have_posts()){
while($q->have_posts()){
$q->the_post();
wp_redirect(get_permalink());
die();
}
} else {
// oh dear, there isnt a post with this 'password', put a redirect to a fallback here
wp_redirect('http://www.google.com');
die();
}
wp_reset_query();
}
}
add_action('init','doPasswordStuff');
Run Code Online (Sandbox Code Playgroud)
但是我在这一行上遇到致命错误(致命错误:在非对象上调用成员函数get_var()):
$ post_id = …