当用户未登录时,我对单个帖子的ajax请求有问题。这篇文章是一个日历日期,是在未来。如果我登录wordpress,查询工作正常,但当我注销时,查询工作不正常。当不涉及ajax时,这种类型的查询也可以工作,例如,如果我想在类别页面中显示所有未来的日期。我是否需要启动wpdb实例来获得结果?必须经过许可,但找不到任何答案。
function ajax_get_date_post()
{
// get the submitted parameters
$post_id = $_POST[\'post_id\'];
$args = array(
\'post_type\' => \'mandy_dates\',
\'p\' => $post_id,
\'post_status\' => \'future,publish\'
);
// build and make query
$clicked_date = new WP_Query( $args );
// response output
header( "Content-Type: application/html" );
if ( $clicked_date->have_posts() ) {
while ( $clicked_date->have_posts() ) {
$clicked_date->the_post();
include( $this->get_single_post_template_path() );
}
} else {
echo "Sorry, Date not found";
}
exit;
}
更新时间:
这些是挂钩:
add_action( \'wp_ajax_nopriv_ajax_get_date_post\', array(&$this, "ajax_get_date_post") );
add_action( \'wp_ajax_ajax_get_date_post\', array(&$this, "ajax_get_date_post") );
这是Ajax JS:
function ajax_get_post( _id )
{
$.post(
mandy_dates_js_config.ajaxurl,
{
action : \'ajax_get_date_post\',
post_id : _id
},
function( response ) {
on_date_loaded( response );
}
);
}
更新2:
我想出来了。修改了“在单个帖子上显示将来的帖子”以用于我的ajax请求,这里是更新的代码
function ajax_get_date_post()
{
// get the submitted parameters
$post_id = $_POST[\'post_id\'];
$args = array(
\'post_type\' => \'mandy_dates\',
\'p\' => $post_id
// \'post_status\' => \'future,publish\'
);
// build and make query
$this->clicked_date = new WP_Query();
add_filter( \'the_posts\', array( &$this,\'show_future_posts\') );
$this->clicked_date->query( $args );
remove_filter( \'the_posts\', array(&$this,\'show_future_posts\') );
// response output
header( "Content-Type: application/html" );
if ( $this->clicked_date->have_posts() ) {
while ( $this->clicked_date->have_posts() ) {
$this->clicked_date->the_post();
include( $this->get_single_post_template_path() );
}
} else {
echo "Sorry, Date not found";
}
exit;
}
function show_future_posts( $posts )
{
global $wpdb;
if( $this->clicked_date->post_count == 0 ) {
$posts = $wpdb->get_results($this->clicked_date->request);
}
return $posts;
}