• gresakg

    (@gresakg)


    Hi, I am adding features on a production site running bbpress and budypress, I don’t have full control over. I wrote a custom query

    $query = new WP_Query([
    'post_status' => 'pending',
    'post_type' => ['topic', 'reply'],
    'meta_key' => '_bbp_forum_id',
    'meta_value' => $this->moderated_forums,
    'posts_per_page' => 10,
    'orderby' => 'date',
    'order' => 'ASC'
    ]);

    Since there was a plugin that changed my query using pre_get_posts hook kind of the wrong way, I had to override variables it set like this.

    if( isset($query->query['post_type']) 
       && $query->query['post_type'] == ['topic','reply'] 
       && $query->query['post_status'] == 'pending' 
       && !is_admin()) {
          $query->set('post_status','pending');
          $query->set('posts_per_page',10);
    }
    
     return $query;

    At the end when dumping the query just before the loop I got the correct query:

    request: """
        SELECT SQL_CALC_FOUND_ROWS  p04d_posts.ID
        FROM p04d_posts  INNER JOIN p04d_postmeta ON ( p04d_posts.ID = p04d_postmeta.post_id )
        WHERE 1=1  AND ( 
         ( p04d_postmeta.meta_key = '_bbp_forum_id' AND  p04d_postmeta.meta_value IN ('6') )
        ) AND p04d_posts.post_type IN ('topic', 'reply') AND ((p04d_posts.post_status = 'pending'))
      GROUP BY p04d_posts.ID
      ORDER BY p04d_posts.post_date ASC
      LIMIT 0, 10
      """

    Which is exactly what I expected. However, there are 16 posts in the query object (notice the limit 10), and the first 6 posts do not match the query at all (post_status is not ‘pending’). This is from the same dump as above (same wp_query object). As you can see, there are 16 posts and the first one has post_status “publish”.

      +posts: array:16 [▼
        0 => WP_Post {#8738 ▼
          +ID: 118311
          +post_author: "0"
          +post_date: "2002-07-27 15:10:24"
          +post_date_gmt: "0000-00-00 00:00:00"
          +post_content: " ... post content not important ... ?"
          +post_title: ""
          +post_excerpt: ""
          +post_status: "publish"
          +comment_status: "open"
          +ping_status: "open"
          +post_password: ""
          +post_name: "118311"
          +to_ping: ""
          +pinged: ""
          +post_modified: "2002-07-27 15:10:24"
          +post_modified_gmt: "0000-00-00 00:00:00"
          +post_content_filtered: ""
          +post_parent: 118279
          +guid: ""
          +menu_order: 3
          +post_type: "reply"
          +post_mime_type: ""
          +comment_count: "123934"
          +filter: "raw"
        }

    The same goes for all first 6 posts, the last 10 posts are what I was queriing for.

    Now the question: where do the first 6 posts come from? How can I pre-sanatize results? Is this a bug in WordPress?

    I’ll be glad to hear any suggestions.

    • This topic was modified 1 year ago by gresakg.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s possible to alter a posts array after the query has been made. Any theme or plugin could be responsible. Also, sticky posts can influence the posts per page count unless you use the 'ignore_sticky_posts' => true, arg.

    If a theme or plugin is manipulating the posts array, you should try to identify which is responsible.

Viewing 1 replies (of 1 total)
  • The topic ‘Unexpected query results’ is closed to new replies.