1

I was wondering if this is possible. I created a client portal where they can see all of their work that was assigned to them (this is all being displayed on the frontend). Once they login, they are then redirect to their page. I am using the code below to display the projects related to the current user

 <?php if ( is_user_logged_in() ) : function filter_posts_by_author( $query ) { 
        global $current_user; get_currentuserinfo();
        $query->set( 'author', $current_user->ID );
      }
        add_action( 'pre_get_posts', 'filter_posts_by_author' );?> 
 <h2>Post Goes here</h2>
 <?php else: wp_die('Sorry, you do not have access to this page. Please <a href="/#/">sign in</a> to view this page.');endif; ?>

This works perfect. The problem I am trying to figure out is how to display all post from all user when you are login as an administrator (on the front end)

I tried this but it then displays all of the post to all clients and administrator

 <?php if ( is_user_logged_in() ) : function filter_posts_by_author( $query ) { 
        global $current_user; get_currentuserinfo();
        $query->set( 'author, administrator', $current_user->ID );
      }
        add_action( 'pre_get_posts', 'filter_posts_by_author' );?> 

How would I go about displaying all post once an administrator is login and only display selected post when a user login?

1

You could try this, IF the current user is logged in AND not Adminstrator it will pull all posts from the current user, otherwise it should default and show all posts from all users.

<?php 
    function filter_posts_by_author( $query ) {
        if( is_user_logged_in() ) {
            if( !current_user_can( 'administrator' ) ) {
                global $current_user; 
                get_currentuserinfo();
                $query->set( 'author', $current_user->ID );
            }
        }
        else {
            wp_die('Sorry, you do not have access to this page. Please <a href="/#/">sign in</a> to view this page.');
        }
    }
    add_action( 'pre_get_posts', 'filter_posts_by_author' );
?>
  • perfect thank you! This works perfect but I am unable to use <?php else: wp_die('Sorry, you do not have access to this page. Please <a href="/#/">sign in</a> to view this page.');endif; ?> anymore. How would I go displaying this message if someone access this page without logging in? @Howdy_McGee – user3756781 Aug 29 '14 at 14:04
  • @user3756781 You just need to split your IF statement out, see my updated answer. – Howdy_McGee Aug 29 '14 at 14:06
  • that is giving me an error on the else { @Howdy_McGee – user3756781 Aug 29 '14 at 14:08
  • @user3756781 whoops, was missing my bracket on the 2nd IF - try that! – Howdy_McGee Aug 29 '14 at 14:08

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.