0

Does WordPress have any functions which will help me create a system to have data ( eg custom posts ) associated with a particular user and (by default) not visible to any other user. I have had a look around and I haven't seen anything really like this so I am starting to suspect that it's outside of WordPress's domain but I thought I'd just ask here to make sure I wasn't missing anything obvious. I haven't fully checked out all the capabilities of the membership plugins because I believe they focus of allowing all ( or tiered ) content to logged in members ... so I may be wrong in this.

What I need is something like a membership scenario where members can only see their own content, and not anyone else's. Is it possible to do something like this with WordPress, ideally with the core functions or better still a plugin. I am thinking that the user table is not designed for this type of activity.

I found this article, and I know it's pretty old, but from this I sort of derive that adding twitter usernames and the like are about as far as we can go.

1

The basis for making this work is actually pretty simple. Essentially, you just need to hook pre_get_posts and set the author query var to the value of the currently logged in user for each type of query you want to limit results on.

function logged_in_user_posts( $query ){
    if ( is_user_logged_in() ){
        global $current_user;
        get_currentuserinfo();
        $query->set( 'author', $current_user->ID );
    } else {
        // force query to return nothing if user not logged in
        // you may want to handle this more elegantly
        $query->set( 'author', -1 );
    }
}
add_action( 'pre_get_posts', 'logged_in_user_posts' );

Now, you'll probably want to use some conditional tags here to limit what sorts of queries this acts on. For example, this will cause pages to 404 as-is, so you probably want to make sure the query is not for a page:

if( ! $query->is_page() )

EDIT- after a re-read of your question, I think perhaps I've misunderstood what you're trying to do...

  • No I think you might be understanding me correctly all right and I might have a go at trying to get the above working (thanks for that) but still wouldn't this still leave the individual posts open to anyone who could get the the url. I mean if I know the id of a post is it not possible to view it even if I'm not the owner. As far as I can see we are only limiting whats shown in the query but I'd need the material to be inaccessible to anyone but the originator. Maybe I have a fundamental hole in my WordPress understanding here. I am not very good on security to be honest. – byronyasgur Nov 4 '13 at 15:16
  • a single post view is a query just as a list of posts is, this action is invoked on every query, front and back end. – Milo Nov 4 '13 at 15:40
  • Oh I see, sorry. I will try it out. – byronyasgur Nov 5 '13 at 0:26
  • It's still returning the page if the user is not logged in. It's like as if '$query->set( 'author', -1 );' is having no effect for some reason. – byronyasgur Nov 5 '13 at 0:38
  • If I've understood the WP_query page properly then set('author', -1) would display all authors except author id=1 ... so I tried $query->set( 'author_name', 'thisauthordoesnotexist' ); ... which seems to work, but I hope there's a better way of setting wordpress to display no posts. Would you know of one. – byronyasgur Nov 5 '13 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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