I’m running a query in WordPress and need to re-use the $my_query_results
variable later on in my script.
function init() {
$args = array(
'post_type' => 'post'
);
$my_query_results = new WP_Query( $args );
}
–
function process() {
// I need to process $my_query_results here.
}
add_action( 'wp_ajax_myaction', 'process' );
I don’t want to re-run the query inside process()
. How can I make $my_query_results
available to the process()
function?
Background info: The process()
function handles data sent via an AJAX request. After processing, it sends a response to the browser. For example: echo json_encode( $response )
If these functions are present in the same class you could assign it to a class property:
You could pass the variable as a param
usage
or You can simply do global variable:
}