在未登录时,AJAX未来的单次发布查询不起作用

时间:2011-01-18 作者:Thomas

当用户未登录时,我对单个帖子的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;
}

1 个回复
SO网友:Bainternet

通常是因为您缺少对未登录用户的“add\\u action”调用。

将其添加到插件或函数中。php

add_action(\'wp_ajax_nopriv_REPLACETHIS\', \'ajax_get_date_post\');
并将REPLACETHIS更改为ajax调用中的动作值。

请记住,当从前端调用ajax时,如果您希望不允许任何登录用户进行这些调用,则始终会添加这两个选项:

    add_action(\'wp_ajax_my_action\', \'my_action_callback\');
    add_action(\'wp_ajax_nopriv_my_action\', \'my_action_callback\');
您可以在codex

结束