If I use the following function in any theme template, it works as expected. However when trying to use it within an AJAX function in my functions.php, it returns empty.
$args = array (
'numberposts' => 10,
'post_type' => array('topic','reply'),
'author' => 454
);
$user_posts = get_posts($args);
Any ideas?
Here is the full function:
function he_delete_user(){
$args = array (
'numberposts' => 10,
'post_type' => array('topic','reply'),
'author' => 454
);
$user_posts = get_posts($args);
if (empty($user_posts)){
$response_array['status'] = 'false';
echo json_encode($response_array);
}
else{
// delete all the user posts
foreach ($user_posts as $user_post) {
wp_delete_post($user_post->ID, true);
}
$response_array['status'] = 'success';
echo json_encode($response_array);
}
}
add_action('wp_ajax_he_delete_user','he_delete_user');
This is a bandaid, not a fix. But it seems there is a bug within WP_Query, so I had to modify the query right before it was actually run in order for the post_type parameter to not be ignored.
1) I changed get_posts() to a WP_Query
2) I added this to my functions.php file:
3) Then I modified the query with that action right before it was run and then remove the action right after so it would not affect any other queries: