
As usage of the Members Area in WooCommerce Memberships, we’ve seen some interesting customizations of how this members-only information is shown to customers and members.
One of the most common customizations we’ve seen people do or ask for guidance on is the ability to sort or adjust what’s shown under My Content, so let’s take a look at how you can adjust that in the WooCommerce Memberships Members Area.
Available Hooks
You can filter the list of My Content, My Products, or My Discounts within the member area from the same hook: wc_memberships_get_restricted_posts_query_args
. We’ll focus on the “My Content” section in this tutorial, but it provides a base for how you’d adjust other sections of the Members Area.
Here’s the doc block for the hook we want:
/** * Filter restricted content query args * * @since 1.6.3 * @param array $query_args Args passed to WP_Query * @param string $query_type Type of request: 'content_restriction', 'product_restriction', 'purchasing_discount' * @param int $query_paged Pagination request */ $query_args = apply_filters( 'wc_memberships_get_restricted_posts_query_args', $query_args, $type, $paged );
Notice that you can check for different types of queries, then filter the arguments present in these as needed.
Let’s walk through a couple of examples on adjusting query arguments.
Sorting My Content Posts
By default, the posts and pages under “My Content” are sorted with the newest content at the top.
Let’s make a small tweak to this to show the content from oldest to newest instead. We’ll just need to adjust the query order and orderby arguments to do so.
First, I’ll bail out if we’re not adjusting the “My content” query, then I’ll adjust these arguments.
/** * Sorts posts in "My Content" by date from oldest to newests instead of newest first (default). * * @param array $query_args args for retrieving membership content * @param string $type Type of request: 'content_restriction', 'product_restriction', 'purchasing_discount' */ function sv_wc_memberships_sort_my_content( $query_args, $type ) { // bail if we're not looking at "My Content" if ( 'content_restriction' !== $type ) { return $query_args; } $query_args['order'] = 'ASC'; $query_args['orderby'] = 'date'; return $query_args; } add_filter( 'wc_memberships_get_restricted_posts_query_args', 'sv_wc_memberships_sort_my_content', 10, 2 );
Now my content uses oldest-to-newest sorting instead:
This is a pretty simple example of changing query parameters, but this gives us a lot more power of which content we show and how.
Changing Post Types Included in My Content
Now let’s make a slightly larger change. By default, Memberships will show any non-product content that’s restricted under “My Content”. If you use other plugins, such as bbPress or portfolio plugins, this means that this content will all be in the same list.
If you want to remove some content, you can specify which post types to include instead. Let’s take this back to only posts and pages instead of all content.
/** * Only includes posts and pages in "My Content" section, allowing other post types * to be output in custom sections. * * @param array $query_args args for retrieving membership content * @param string $type Type of request: 'content_restriction', 'product_restriction', 'purchasing_discount' */ function sv_wc_memberships_adjust_my_content_query( $query_args, $type ) { // bail if we're not looking at "My Content" if ( 'content_restriction' !== $type ) { return $query_args; } $query_args['post_type'] = array( 'post', 'page' ); return $query_args; } add_filter( 'wc_memberships_get_restricted_posts_query_args', 'sv_wc_memberships_adjust_my_content_query', 10, 2 );
Now we’re back to regular content, no bbPress forums or topics included:
If you really want to be advanced, you could then move this content into a custom Members Area section so it’s displayed by itself.
Taking It Further
We have plans to make basic customizations for the Members Area easier, but we’ll likely wait until we can require WooCommerce 2.6 to do so. Right now we have a lot of compatibility code needed to make it available in WooCommerce 2.5 (before the tabbed account was added), so we can streamline a lot when we require WooCommerce 2.6 later in 2017.
In the meantime, you can view the full snippets in our collection, and hopefully this gets you started with your own changes!
Need some help taking this code further? We recommend Codeable for small custom projects.
Thank you this share, worked good
Hi, Thanks for the great snippets.
Is there a way I can create a members-dashboard to show all members (parent) pages.
I need to show a list of all member pages somewhere else then in my content.
I’ve created a dashboard for the members with categories. If they click one category, they should see a page with all the pages the may see.
Thanks so much!
Kind regards, Bas
Thank you for adding this. When we last looked at Membership Extension this was a big missing piece that prevented us from moving to Membership Plugin. We will take another look at how we can use this plugin to manage our restricted content.
Question: Would it be possible to get code snippet to retrieve URLs by post type for “My Content”. This way I can display this in anyway I want in the custom member area or any other pages I create.
Hi, where could I find what are the other fields I could use for sorting? On the top of the ‘day’. I would like to show these on the order of access given – for example.