• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Display query loop block entries only to active Easy Digital Downloads subscribers

Find out how to get WordPress native query loop block entries to be displayed only to users with an active Easy Digital Downloads subscription.

If you have a membership created with Easy Digital Downloads (plus the Recurring Payments and Content Restriction addons) you’ll know that it’s pretty easy to restrict individual content of any post type.

But one of the limitations is that lists of entries, such as the one on a CPT archive page, are visible to all users.

Right now the easiest way to display a list of posts is with the native query loop WordPress block. In fact, in this tutorial I go through all the options it offers in its user interface.

Thanks to WordPress 6.1 version introducing a PHP filter that allows you to interact directly with the query, now you can modify it programmatically.

In today’s tutorial you will see how, by combining the filters query_loop_block_query_vars and pre_render_block and accessing the class EDD_Recurring_Subscriber, you will be able to display the entries only to users who have an active subscription.

Let’s see how:

Steps to restrict the display of the query loop block to users without an active EDD subscription

1. Create and customize the query loop block

First, add a query block and configure the settings to your liking so that it displays the entries you want.

If you are not yet familiar with the interface I recommend you take a look at the tutorial where I explain all the options that the query loop block allows.

2. Add a custom CSS class

Once you have created the block, go to the “Advanced” section and add restricted in “Additional CSS class(es)” of the “Post Template” block.

structure-block-list-contents-WordPress
add-custom-class-restricted-editor-wordpress

3. Modify the query loop block query

Add the following code at the end of functions.php or in your functionality plugin:

To see this and another 965 code snippets of this website, login or subscribe here.

In this code you use the filter pre_render_block, which allows you to access the attributes of all blocks, along with query_loop_block_query_vars in which you modify the query.

The conditionals you use are:

  • !$active_subscriber: the user does not have an active subscription (since you use ! before the variable). You get the variable using the class EDD_Recurring_Subscriber.
  • $parsed_block['attrs']['className'] === 'restricted': the CSS class of the block is restricted.

That is, if the user does not have an active subscription and the block has the CSS class restricted it will not show any entry. Instead, for active subscribers it will show the complete listing, just as you customized it.

In this case, you use post__in to indicate not to show any entries. But you can access any parameter of WP_Query, so you can use any strategy you want.

However, keep in mind that since you are using a PHP filter you have the limitation that you will only see the result in the web frontend, in the backend the changes will not be reflected.

Conclusions

As you can see, you can use the native WordPress query loop block to display the posts you want with the custom design and only to users who have an active Easy Digital Downloads subscription.

Any questions? I read you in the comments.

And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉

Block Editor Easy Digital Downloads PHP Query loop WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?