I'm having troubles with Woocommerce. I need to sell videos. I've created virtual products with a link to a different post for each (no download as I prefer to embed the video on my website and to use streaming from YouTube/Vimeo). I would like that, once the user buy a specific video, he receives the link to a specific post page. Until here, I know to do it. But this page should be visibile only if he already bought the link. So if he shares the link with someone else or if someone find the link, he won't be able to see the embedded video. I can't do it with roles because I would need to create a role for each video.

So I need to have a loop in which I compare the current page ID with the custom ID (or a custom field I can add to each product) of every product bought by the logged user.

if (current page id == custom ID of product bought) than show the content of the post.

Is it possible to do something like this?

Thank you!

2 Answers 2


Yes, it's possible.

Basically, you'll need to create a theme template for whatever post type the videos are being shown in. (If it's a Page, create a page template; if it's a custom post type, create a template for that post type). You will need to work out the specific query and code depending on your plugins, but basically:

// First check if the user is logged in
if(is_user_logged_in()) {
    // If they're logged in, show the sitewide header
    // If they're logged in, have they bought this particular product?
    // First identify the product ID
    // Then check if they've bought that product,
    // and save to a variable called $user_has_purchased
    // Then, add conditions for if they have bought it, and if they haven't
    // If they have:
    if($user_has_purchased == true) {
        // Now add your code to show the content, which may just be the_content
        if ( have_posts() ) :
            while ( have_posts() ) : the_post();
    } else {
        // Else here means they are logged in but haven't purchased
        // You could either show them a message with a link, or auto-redirect
        // to the product page to encourage them to buy it
        echo "Sorry, this content is only available by purchase.";
    // Show the sitewide footer
} else {
    // Else here means they are not logged in
    // So you could either link to the login form, or redirect them there
    wp_redirect( wp_login_url() ); exit;

(Note that the header and footer are only shown if the user is logged in, because the "else" says to redirect them. You can't redirect them if you have already output the header.)

It may be easiest if your video-showing pages are all a custom post type. As long as you have a single-posttype.php set up with these conditions, every time you create one of those CPTs, it will always check for login and for purchase. If instead you create a Page Template that you have to apply manually for each page, it would be easier to accidentally forget to set the template, and thus make your content public by accident.


Piggybacking on WebElaine for the $user_has_purchased portion:

Create a post object field that only accepts products and attach it to a user. Now you can push specific products that a user purchases into this field. When a user goes to a product's single page, you can query the current user and check this field to see if it contains the current product's ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.