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

admin2025-01-08  5

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.

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.

Share Improve this question edited Aug 18, 2015 at 12:20 Firsh - justifiedgrid.com asked Aug 18, 2015 at 12:15 Firsh - justifiedgrid.comFirsh - justifiedgrid.com 5892 gold badges6 silver badges20 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

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'
) );

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

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...

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268342a1267.html

最新回复(0)