Yet another programmer blogging about code

Stop Role Scoper messing with the found_posts of custom queries

Role Scoper is a very handy WordPress plugin for websites that need to manage access to pages for a range of different user classes. But it has a nasty habit of getting in the way sometimes, and it can mess up the pagination of a custom WP_Query by changing the number of found posts (found_posts). Here’s how to tell it to leave you alone!

I just wrote a simple plugin to list posts in a category, with pagination. It uses the function paginate_links() which neatly generates a list of page links for you, for basically any WordPress post query. When I was testing it while logged in, all was well. But when I checked in another browser (not logged in), it generated too many page links: 36 instead of 10! It seemed that somehow the query was finding 356 results instead of the true number, 92.

After a bit of poking around, on a hunch I deactivated Role Scoper… and the problem disappeared. A quick grin told me what I needed to know: Role Scoper was changing the number of found posts in the query. It does this in the ScoperTeaser, which creates “teaser” text for pages/posts that require more access privileges than the visitor has.

Luckily, the Role Scoper author had already built in a way to tell it not to do this. There is a flag that can be set to tell Role Scoper not to bother with a teaser, so I just needed to set that before running my custom query:

// look for Role Scoper plugin, which messes with the query result counts
global $query_interceptor;
if (isset($query_interceptor) && class_exists('QueryInterceptor_RS')) {
    // tell Role Scoper to stop messing with our query result counts!
    $query_interceptor->skip_teaser = true;

There, job is not undone!