What would cause a basic get_posts()
query to return an empty array during an Ajax function?
I have a front end page which loads a form. There is a dropdown which is populated when the page loads with some post titles. The same function that populates the select list values is called via Ajax when the value of another select list changes.
When the function is called via Ajax, the result is an empty array. When the page loads, the select list is fully populated.
By usingerror_log( var_export( $args, true ), 0 );
I have confirmed that the arguments passed to the function are the same on both page load and Ajax call and with error_log( var_export( $addons, true ), 0 );
I confirmed results on page load and an empty array on Ajax call. This also confirms execution of the correct function via Ajax.
/**
* Retrieve all addons.
*
* @since 1.4
* @param arr $args Array of arguments. See @get_posts.
* @return arr|bool Addons.
*/
function mh_get_addons( $args = array() ) {
$defaults = array(
'posts_per_page' => -1,
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'mh-addon',
'post_status' => 'publish'
);
$args = wp_parse_args( $args, $defaults );
error_log( var_export( $args, true ), 0 );
$addons = get_posts( $args );
error_log( var_export( $addons, true ), 0 );
return apply_filters( 'mh_get_addons', $addons );
} // mh_get_addons
Here is the Ajax function. As you can see I am passing no args here whilst testing so I would expect to see the same results I do on the initial page load.
function mh_update_form_addon_options() {
$addons = mh_get_addons();
if ( ! empty( $addons ) ) {
$result['type'] = 'success';
$result['addons'] = $addons;
} else {
$result['type'] = 'success';
$result['addons'] = 'No addons';
}
echo json_encode( $result );
die();
} // mh_update_form_addon_options
add_action( 'wp_ajax_mh_update_form_addon_options', 'mh_update_form_addon_options' );
add_action( 'wp_ajax_nopriv_mh_update_form_addon_options', 'mh_update_form_addon_options' );
`
UPDATE
The function is now returning results via the Ajax call when a user is logged in. However, if not logged in, no results. Strange thing is that the initial call to the mh_get_addons()
function which is initiated on page load without ajax returns results regardless of whether or not a user is logged in...
var_dump( __FUNCTION__ );
at the beginning of the callback to check if anything at all happens – check the console for output.init
hook. The extension's class is instantiated with theplugins_loaded
hook. The page displayed is via a shortcode usingadd_shortcode
. The shortcode instantiate's a further class within which a method is called and in that method I am usingwp_enqueue_script()
andwp_localize_script()
for the JS file. The JS file waits for a change to be triggered on a form input field and then processes the call to ajax. I am seeing No Addons as the returnecho json_encode( array( 'type' => 'success', 'addons' => 'Testing' ) ); die();
Testing is returned. Additionally, per themh_update_form_addon_options()
function above, I get No Addons if theget_posts()
result is empty