• 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 Restrict Content Pro active subscribers

Find out how to get WordPress native query loop block posts to only be displayed to users with an active Restrict Content Pro subscription.

Restrict Content Pro is a very handy and versatile plugin for restricting individual content of any post type.

But one of the limitations is that posts lists, like a CTP archive page, are visible to all users.

That is, even if subscribers do not have access to “Entry X”, it will be displayed on the archive page. However, when they click on it, they will get the message that they do not have access to it.

But a more user-friendly option would be to hide the content to which the users do not have access and thus avoid frustration.

To solve this limitation I created a couple of tutorials:

  • Display only the entries that the Restrict Content Pro subscriber has access to
  • Display only the entries of a certain category to which the Restrict Content Pro subscriber has access

These are interesting options but they do not allow you to design the query loop from the editor user interface.

That is solved with the WordPress native block that allows you to display a list of entries (query loop). In fact, in this tutorial I review all the options offered in its user interface.

However, WordPress 6.1 version introduces a PHP filter that allows you to interact directly with the query, so the possibilities are extended almost to infinity.

In today’s tutorial you will see how by combining the filters query_loop_block_query_vars and pre_render_block with the function rcp_user_has_active_membership you will be able to display posts only to users who have an active subscription.

Let’s see how:

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

1. Create and customize the query loop block

First, add a query loop block and configure its 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 of the query loop block

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

To see this and another 883 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:

  • !rcp_user_has_active_membership: the user does not have an active subscription (since you use ! before the function).
  • $parsed_block['attrs']['className'] === 'restricted': the block CSS class is restricted.

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

In this case, you use post__in to indicate not to show any entries. But you can access any WP_Query parameter, 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 be able to see the result in the web frontend, in the backend the changes will not be reflected.

Conclusions

You can use the native WordPress query loop block to display the posts you want with a custom design and set that only users who have an active Restrict Content Pro subscription can see them.

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 PHP Query loop Restrict Content Pro 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?