1

I have 2 user groups: authors and clients

Authors can publish posts and specify in metabox for wich client these post is ment for.

Client can publish posts and view, edit posts, created by Authors for them.

Problem:

In wp-admin on posts screen ( wp-admin/edit.php ) i need to display:

if Author: only author posts

if Client: client posts and posts where meta value is client id

if admins: all posts

I'v added pre_get_posts filter to filter only author posts:


/**
 * Show only author posts, but all for admins
**/
function m7_24_ee_posts_for_current_author( $query ) {

    if( $query->is_admin && 'post' == $query->query_vars['post_type'] && ! current_user_can( 'administrator' ) ) {
        global $user_ID;
        $query->set( 'author',  $user_ID );
        if( current_user_can( 'client' ) ) {
            $query->set( 'meta_key', 'client' );
            $query->set( 'meta_value', $user_ID );
        }
    }
    return $query;
}
add_filter( 'pre_get_posts', 'm7_24_ee_posts_for_current_author' );

But the problem is that meta_key and author fields acts like AND, but i need them to be 'OR`. or author is $user_ID or meta_value is $user_ID

If there's no easy solution for these than it can be made by marging 2 queryies ( 1 with author and 2 with meta value ), but the problem is that i don't know which hook should i use to marge them, becouse pre_get_posts is fiering to early.

  • may be, but since i'm working in dashboard ( wp-admin/edit.php ) screen, there's less chance of writing custom code. And if you understand my question i was asking where ( how ) ( wich hook to use ) to write these code, becouse pre_get_posts doesn't fits.. – Михаил Семёнов Jan 4 '15 at 22:55
  • Please (1) format your question properly (WYSIWYG editor) and (2) show us what you have tried and there you failed. By now it's just a task and nothing that can be worked on. – kaiser Jan 4 '15 at 23:25
  • updated, added code samples, hope it helps to understand my problem. – Михаил Семёнов Jan 4 '15 at 23:45
1

Here's a hack for the main post query in wp-admin that should do the trick:

/**
 * Modification of the wp-admin main (post) query: 
 *     If current user has the "client" role then 
 *     show client's posts OR posts where the "client" meta value is the client id.
 *
 * @see http://wordpress.stackexchange.com/a/173967/26350
 */

function wpse_pre_get_posts( $q )
{
    if( is_admin()
        && $q->is_main_query()
        && 'post' === $q->get( 'post_type' ) 
        && ! current_user_can( 'administrator' )  
    ) 
    {
        $q->set( 'author',  get_current_user_id() );
        if( current_user_can( 'client' ) ) 
        {
            $q->set( 'author',  null );
            $q->set( 'meta_key', 'client' );
            $q->set( 'meta_value', get_current_user_id() );
            add_filter( 'posts_where', 'wpse_posts_where' );
        }
    }
    return $query;
}
add_filter( 'pre_get_posts', 'wpse_pre_get_posts' );

where the callback for the posts_where filter is defined as:

function wpse_posts_where( $where )
{
    global $wpdb;
    remove_filter( current_filter(), __FUNCTION__ );
    return str_ireplace(
        "{$wpdb->postmeta}.meta_key",
        sprintf( 
            "{$wpdb->posts}.post_author = %d 
             OR {$wpdb->postmeta}.meta_key", 
            get_current_user_id() 
        ),
        $where
    );
}

where I assume there are no other meta queries on the main post query in wp-admin. If that's not the case, then you should be able to refine this further.

  • 1
    Totaly forgot we can query database :(( should figure it by my self. Thanks a lot, worked) – Михаил Семёнов Jan 5 '15 at 15:19

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.