0

In my_method() below, I'm assigning an array of post IDs to the property $this->post_ids which is to be used later on in my query args - see process().

If I hook my_method() to the init action hook, things work but if I hook my_method() to the wp action hook, things don't work.

When my_method() is hooked to wp, doing var_dump( $this->post_ids ) inside process() displays null.

When my_method() is hooked to init, doing var_dump( $this->post_ids ) inside process() displays Array[15].

-

public function __construct() {
    // This works
    add_action( 'init', array( $this, 'my_method' ) );

    // This doesn't work.
    add_action( 'wp', array( $this, 'my_method' ) );

    add_action( 'wp_ajax_myaction', array( $this, 'process' ) );
    add_action( 'wp_ajax_nopriv_myaction', array( $this, 'process' ) );
}

-

public function my_method() {
    $this->post_ids = explode( ',', '69,70,71,72,73,80,86,89,90,91,92,93,94,96,169' );
}

-

public function process() {

    $args = array(
        'offset' => 10,
        'post_type' => 'post',
        'post__in' => $this->post_ids
    );

    $this->query = new WP_Query( $args );

    // Process the query results here...
}

Background info: The process() method handles data sent via an AJAX request. After processing, it sends a response to the browser. For example: echo json_encode( $response )

My question:

Inside process(), why is the value of the $this->post_ids property:

  • null when I hook my_method() to wp

  • Array[15] when I hook my_method() to init?

I don't understand why it is different.

Update: The value of $this->post_ids is null only inside the process() method. Could this be because process() is running during an AJAX request?

  • I think that your are right. process() is running during an Ajax request, not during a WP object request. The wp action occurs after a WP object has been set up, so it is not available during Ajax requests, so my_method is never executed if it is hooked to wp action. By the way, don't forget to exit at the end of the Ajax processing or use the wp_send_json family of functions which sends a json response and performs the exit for you. – cybmeta Sep 12 '14 at 13:49
  • Thanks @cybmeta, it looks that way. It's a pain for me because inside my_method() I also need to get the author ID of the author page currently being viewed. But author ID isn't available at the point init is run. So I can't hook my_method() to init. But that's another question :) Thanks again. – henrywright Sep 12 '14 at 14:14
  • In the init action hook, or any other action hook, executed during the Ajax request you won't get the author ID or any other information of the page/post where you send the Ajax request. That is normal. You need to pass that information tin the Ajax request data. – cybmeta Sep 12 '14 at 14:17
  • This particular Ajax call happens after a user has clicked a button so the init action hook will be executed before the Ajax request takes place. – henrywright Sep 12 '14 at 14:23
  • Yes, the init action occurs in the Ajax request but the Ajax request is not he request for the page where you send the Ajax from, so the information of that page is not available if you don't send it with Ajax data. Why a particular page should be loaded during an Ajax request? I think you are confused here. Other thing is if you request a particular page via Ajax but this not your situation. – cybmeta Sep 12 '14 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.