Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am having a problem with an ajax request for a single post, when the user is not logged in. The post is a calendar date and is in the future. the query works fine if I am logged into wordpress but doesn't when I am logged out. This type of query also works when there is no ajax involved, for example if I want to display all future dates in a category page. Do I need to fire up an instance of wpdb to get the results? Must be a permission thing but cant find any answers.

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;
}

UPDATE:

these are the hooks:

    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") );

this is the 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 );
        }
    );
}

UPDATE 2:

I figured it out. modified the "show-future-posts-on-single-post" to work in my ajax request, here the updated code

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;
}
share|improve this question
    
maybe the problem is outside this function? –  onetrickpony Jan 18 '11 at 11:56
    
i logged the query and the object is right, just the posts array is empty, as soon as I am logged in it works perfectly. –  Thomas Jan 18 '11 at 22:01
    
I think it might have something to do that the posts are in the future. it works perfect as soon as the posts are from the past. there is this plugin (plugins.svn.wordpress.org/show-future-posts-on-single-post/… ) which seems to fix the problem by using wpdb, but since its an ajax request I don't have all the parameters for wpdb available, such as db_username and pass. –  Thomas Jan 18 '11 at 22:06

1 Answer 1

usually it because you are missing an "add_action" call for none logged in users.

add this to your plugin or functions.php

add_action('wp_ajax_nopriv_REPLACETHIS', 'ajax_get_date_post');

and change the REPLACETHIS to your action value from the ajax call.

remember that when calling ajax from front-end and you want to allow none logged in users to make these calls you allways add both:

    add_action('wp_ajax_my_action', 'my_action_callback');
    add_action('wp_ajax_nopriv_my_action', 'my_action_callback');

you can read about more at the codex

share|improve this answer
    
yes sorry should have mentioned that I did this. the function gets called I can evel log wp query and see that all the parameters are set its just that the posts in wp_query array is empty. –  Thomas Jan 18 '11 at 22:03
    
paste you ajax code and your ajax hooks code so i could get a better idea. –  Bainternet Jan 18 '11 at 22:16
    
thanks pasted above. I think the reason why it doesn't work is because the posts are in the future. it works perfectly when I am logged out with dates which are in the past. it only works logged in when the queried posts are in the future. thats the only difference between it working and not working. –  Thomas Jan 18 '11 at 22:57
1  
have you tried using "global $wpdb;" ? –  Bainternet Jan 18 '11 at 23:27
    
thanks בניית אתרים. i figured it out, you are right there is an instance of wpdb already even in an ajax request –  Thomas Jan 18 '11 at 23:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.