get_posts empty when called via Ajax

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…

3 Answers
3

To get data from AJAX call you have to echo or print it. In WordPress you should use wp_send_json() function to send data as response to call.

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 );
    $addons = get_posts( $args );
    wp_send_json( apply_filters( 'mh_get_addons', $addons ) );

} // mh_get_addons

Leave a Comment