I have a custom REST API in WordPress that returns a list of posts using the get_posts function. I am using the PaidMembershipPro plugin, which typically filters the get_posts results according to the registered user's membership level. The filter does not get applied when using get_posts inside my REST API function.

After digging into the code, I see that the plugin hooks into the pre_get_posts and has this conditional logic:

if ( defined( 'REST_REQUEST' ) && false === REST_REQUEST ) {
  // apply user membership filter

It is only applying the filter if the query is not coming from a REST API request.

What's the best way to go about getting this filter applied during REST calls? Should I modify the plugin code directly and remove the REST_REQUEST condition, or is there a less intrusive approach?

  • I ended up creating a new pre_get_posts action in my theme's function.php file, and copied the PMPro filtering function from plugins\paid-memberships-pro\includes\content.php. I changed the REST_REQUEST conditional so that it only executes if it is a a REST request. – rycornell May 31 '19 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.