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 order by 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!