0

So I have been searching for a solution on how I can dynamically change a CPT admin table query by filter the result WHERE Status is equaled to some custom status.

I have searched using function pre_get_posts, but that seems to use ORDERRBY and not WHERE.

I have also searched for other functions such as post_clauses.

Basically, I want to change the results on page load based on the current user's role and only show post that has a certain status. Is this possible?

/*Change results based on the current user's role and status */ 

function posts_for_current_role($query) {
    global $pagenow;

    if( 'edit.php' != $pagenow || !$query->is_admin )
        return $query;

    if( current_user_can('editor')):
      //Add query where clause to status equals "working".
    endif;

    if( current_user_can('administrator')):
      //Add $query WHERE clause to status equals "completed".
    endif;


    return $query;
}
add_filter('pre_get_posts', 'posts_for_current_role');
0

Awesome. This works! Thanks @Milo

/*Change results based on the current user's role and status */ 

function posts_for_current_role($query) {
    global $pagenow;

    if( 'edit.php' != $pagenow || !$query->is_admin )
        return $query;

    if( current_user_can('editor')):
      //Add query where clause to status equals "working".
      $query->set( 'post_status', array( 'working', 'draft' ) );
    endif;

    if( current_user_can('administrator')):
      $query->set( 'post_status', array( 'published') );
    endif;


    return $query;
}
add_filter('pre_get_posts', 'posts_for_current_role');

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.