Why does get_posts only show results for Admins or logged-out users?

I use a get_posts() call with these arguments:

array(6) {
    ["include"]= string(14) "1633,1634,1635"
    ["post_status"]= string(3) "any"
    ["post_type"]= string(10) "attachment"
    ["post_mime_type"]= string(5) "image"
    ["order"]= string(3) "ASC"
    ["orderby"]= string(8) "post__in"
}

While logged out or logged in as an Admin, it works fine and returns the objects for 3 images. However, when the user is just a Subscriber/Author/Editor/Contributor, it returns an empty array.

All plugins enabled/disabled doesn't make a difference.

When switching the theme to an official WP theme, the error no longer persists.

I've dumped all hooks using this (one logged in with subscriber, one with admin), but I find no considerable difference. Case A / Case B. I was assuming that the theme alters my arguments to an extent that there are no results for the altered query. If it's still the case, how and where should it do that?

The theme is Javo Directory.

Topic user-access user-roles get-posts wp-query themes Wordpress

Category Web


just had the same issue. In my case it was very banal to solve: I used W3C Total Cache on this site.

Performance > "Purge all caches" and the query got it's newest results...


You can use wp_set_current_user($user_id); befor calling get_posts() to set desired user, i.e. user with sufficient privileges. See: https://codex.wordpress.org/Function_Reference/wp_set_current_user


try to use a new WP_Query, when you use get_posts you're using the main WP_Query this has hooks that other plugins can use to alter the query. something else i saw is that you're using include key which is not valid

$my_query = new WP_Query( array(
  'post__in'       => array( 1633, 1634, 1635 ),
  'post_status'    => 'any',
  'post_type'      => 'attachment',
  'post_mime_type' => 'image',
  'order'          => 'ASC',
  'orderby'        => 'post__in'
) );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.