Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've developed invoicing system in WordPress for one of my website. I've used custom post type with custom meta field, integrated payment gateway to meet my needs. User generally generate invoice to upload fund. I've used a post submission from in frontend so that user can create an invoice by himself. Everything is running smoothly but one invoice created by an user is visible to other users. Ex. A created invoice, id: APL-2012110489586. User B can access the invoice by typing domin.com?invoice=APL-2012110489586. Now I want to restrict the invoice to be accessed from other user. Only admin and invoice creator can access the invoice. All users are in subscriber role. Need your guideline to perform this job.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

In your Loop, whether for a 'single' page or an 'index' page, check the post author against the current user ID, and only display the post if they match. Assuming you are using a typical WordPress Loop, something like...

global $current_user;
get_currentuserinfo();

// start of the Loop
if ( have_posts() ) {
     while ( have_posts() ) {
         the_post();
         if ($post->post_author != $current_user->ID) continue;

This also assumes that you are inserting your Custom Post Type entries correctly so that all of the data is populated as it should be.

share|improve this answer
    
Actually I want to display to admin and author. Anyway, got the point you mentioned. current_user_can('level_10') || ($post->post_author == $current_user->ID) can work for me. –  Enthusiast Nov 4 '12 at 8:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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