0

So this is what I'm trying to do. I have some posts that are visible only to users with a certain role. I want to display them in a user's profile section, so they're immediately visible after they log-in. Let me underline it: I don't want to display ALL the posts that my user is allowed to see, but only those that are restricted to my user's role.

What I found and tried didn't solve my issue.

$q = new WP_Query(array( 'perm' => 'readable' ));
    while ($q->have_posts()) : $q->the_post();
        the_title();
    endwhile;

Of course this does query all the accessible posts, which is not what I want. So, can I avoid creating a restricted category to solve this?

0

If posts are restricted to a specific role users are required to have, you must add some piece of metadata to the post. Else, WP won't know which posts you mean.

There's a standard box for adding metadata to your post (select 'custom fields' under 'screen option' in your edit screen), or you could create your own metabox or (easier) taxonomy. From your question I gather you already considered the latter option and thought it too cumbersome. But there's no way to escape putting some tag on the post telling the system which ones are meant for which type of user.

Once there is some metadata on the post, you can query for it.

  • It seems like the only thing i can do. Thanks for the idea. – Daniele Squalo Apr 27 '18 at 9:11

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.