Learn in this tutorial how to display only the entries that a Restrict Content Pro subscriber has access to using a shortcode.
The other day an OsomCode subscriber suggested me to create a tutorial combining two previous ones:
- Display only the entries that the RCP subscriber has access to on the archive page of a CPT.
- Display a grid of CPT entries using a shortcode.
In other words, to display, via a shortcode, only the entries that the Restrict Content Pro subscriber has access to.
In addition, you will be able to change the parameter that identifies the CPT of the entries to be displayed in order to customize it for any CPT.
Interesting, isn’t it?
This would be the final result for an input type:
Do you want to see another one? Read on. 😉
Steps to display the CPT entries that an RCP subscriber has access to using a shortcode
1. Restrict access to CPT entries by membership level
First, you must restrict access to Custom Post Type entries depending on the membership level.
To do this, just use the metabox that Restrict Content Pro creates at the end of the entries and select which membership level gives access to that content.
2. Generate the shortcode with parameters to display the CPT entries to which the subscriber has access to
Add the following code at the end of functions.php or in your functionality plugin:
With this code you create a loop with the entries of the CPT
course, by default, or the CTP of your choice as we will see later.
The elements you show are (in order):
- Featured image (linked to the entry)
- Title (linked to the entry)
And you can modify:
- The sorting criteria (in this case by date)
- Descending order (in this case DESC, but you can change to ASC)
- Number of entries to display (in this case 6).
In addition, you add the
rcp_user_can_access conditional to only show the entry if the RCP subscriber has access to the entry.
You encapsulate all this inside a shortcode with
add_shortcode and add the parameter
'cpt', so you can tell it what type of entry you want it to display.
In this case, if you do not pass any parameter to the shortcode, the default CPT will be the course, but if you want it to be another one you only have to change it in:
$default_atts = array( 'cpt' => 'course' );.
You’re almost there.
3. Apply grid layout to the input loop (optional)
Add the following snippet to the end of the style.css file:
With this you get to display the posts in grid format. You can play with the minmax value (250px) to change the number of columns displayed.
Now there is only one step left.
4. Insert the shortcode with the parameter where you want it to go
The advantage of shortcodes is that you can insert them anywhere on the website.
You only have to type “[your_shortcode]” where you want it to be displayed
In this case, as you have to indicate the parameter of the CPT from which you want the entries to be displayed, the shortcode would be something like this:
Do you want to insert entries from another CPT? Then just add the parameter
cpt='', with the name of the CPT.
For example, to display the tutorial CPT entries, you would use this shortcode:
In short, you only have to change the parameter of the shortcode that identifies the CPT by the slug with which you registered the Custom Post Type.
If you want to display only the posts that a Restrict Content Pro subscriber has access to using a shortcode, now you know how to do it.
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. 😉