4

I'm assigning some posts to some users whom are not the author of those posts. Typically private posts are visible only to the post authors. So, if the author has 3 posts, on his logged in edit.php they will get 3 posts. But if another post is assigned to them by custom field (not as a post author), how can I add the post to the post list table?

What I tried so far is: making a function (custom_get_assigned_posts()) to retrieve all the posts that are assigned to a user_id as an array. But then found that there's no easy way including post IDs into the existing query:

function add_assigned_posts( $query ) {
    if( is_admin() && in_array( $query->get('post_type'), array('mycpt') ) ) {
        global $current_user;
        $query->set('post__in', custom_get_assigned_posts( $current_user->ID ) );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'add_assigned_posts' );

But post__in doesn't additionally include things, it actually specified posts to retrieve.

How can I include posts that are assigned to a non-author of posts, and are assigned by postmeta?

  • This is a good question. I'm wondering whether though rather than modifying the admin queries, looking to add capabilities to the user might work? I'm not entirely sure, just a suggestion – Tim Malone May 22 '16 at 6:43
  • Adding capabilities provide access to all the posts, not to specific posts only. I tried that. – Mayeenul Islam May 22 '16 at 8:08
  • You should add that when you say 'What I tried so far' :) – Tim Malone May 22 '16 at 8:09
  • Coming back to this, I think you can do this with capabilities - by filtering them to change them on a per post basis. I've just added an answer, let me know if this solves it for you. – Tim Malone May 22 '16 at 10:20
  • I'd recommend updating the title of your question too - it doesn't explain what you want to do and will make it harder for people to find and help :) – Tim Malone May 22 '16 at 10:39
2

I think you should be able to do this with the user_has_cap filter.

Because that filter passes through the post ID in the $args for the edit_post capability, you can use that to check if the current user has been artificially given this capability - and if so, force the edit_post capability to be true for that instance.

Building from the example in the docs linked above, you'd be looking at something like this:

add_filter( 'user_has_cap', 'wpse_227418_edit_extra_posts', 10, 3 );

function wpse_227418_edit_extra_posts($allcaps, $cap, $args){

  if($args[0] != "edit_post"){
    return $allcaps; // bail out, we're not talking about editing a post here
  }

  if(get_post_meta($args[2], "additional_author", true) == $args[1]){
    // get the meta key 'additional author', checking that it matches the User ID we're looking at the capabilities for
    $allcaps[$cap[0]] = true; // make sure edit_posts is true for this author and this post ID
  }

  return $allcaps;

}

This is pretty quickly put together and is untested so you'd certainly want to test and modify to your needs, but I think this will get you what you want. Of course, adjust the meta_key referred to above to match the key you're after.

References:

  • Tim, nice shot. This filter is really useful. Upvote for that. But I forgot to mention that (probably I forgot many thing :( and disappointed you), I'm using private status for all my posts. But user_has_cap filter works for published and pending. So the post list is already populated with only private posts to that user, I can't push the assigned posts to them. :( – Mayeenul Islam May 22 '16 at 21:13
  • 1
    No, you did mention that - I forgot to take that into account when listing the posts, but thinking about it again perhaps WP is just checking a different capability when loading the posts list? Looking at the capabilities list, perhaps as well as checking for edit_post we also need to check for edit_private_posts, maybe also read_private_posts. Can you do a bit of debugging on that filter and see what is called on the posts page? – Tim Malone May 22 '16 at 21:31
  • How did you go with this @MayeenulIslam? – Tim Malone Jul 6 '16 at 4:07
  • I failed initially. Tried thrice, but failed. Then I altered the query, and granted access to assigned tickets. It may need another shot sometime, but currently not possible for me. – Mayeenul Islam Jul 6 '16 at 8:21

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.