I'm trying to do some work on a custom post type for a specific user role. Basically, if the user is logged in as "Registered For Portal" then I would like that user to only see their own posts listed on the back end. Hoping someone can help.

function control_dfwp($wp_query) {
  $user = wp_get_current_user();
  if (in_array('Registered for Portal',$user->roles)) {
        $wp_query->set( 'author', $current_user->ID );
  • User roles and permissions aren't necessarily relevant to what you're trying to do. What is the code in your question? Does it work? Jan 12 at 15:21

1 Answer 1


Your function looks generally correct, but there are a couple of small mistakes that need to be addressed. Here's a corrected version of your code:

function control_dfwp($query) {
    $user = wp_get_current_user();

    if (in_array('Registered for Portal', $user->roles)) {
        $query->set('author', $user->ID); // Use $user->ID instead of $current_user->ID

add_action('pre_get_posts', 'control_dfwp');

Here are the corrections:

  1. Use $query instead of $wp_query. The parameter passed to the pre_get_posts action is already a query object, so you don't need to use $wp_query->set().

  2. Use $user->ID instead of $current_user->ID. The correct variable to use here is $user, as defined in wp_get_current_user().

Additionally, ensure that this code is placed in your theme's functions.php file or a custom plugin file.

This function hooks into the pre_get_posts action, which allows you to modify the query before it is executed. With this code, if a user with the role "Registered for Portal" is logged in, the query will be modified to only show posts authored by that user.

Remember to test this thoroughly to make sure it behaves as expected and doesn't interfere with other aspects of your site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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