4

Dear Wordpress Captains,

I've stumbled upon a problem and I am seeking for assistance.

What I'm trying to create is a customer portal where the logged in user will only see the posts that are associated to their username. It would be great if this could work on the recent posts page (or index.. The page that a user gets to see first when they logged in, with all the recent posts). I don't have any static pages, besides a contact page (in case this matters).

It seems like a very easy fix, but I have tried every single plugin that I managed to find. For example, one of the plugins I've tried in 'User Specific Content' - but this isn't exactly what I'm looking for as it blocks off the content only when clicked on the post (to open the actual post).. What I'm looking for is a way to already only show the accessible posts for the logged in user on the main page.

There's only one author - the admin, and there's many users (customers, set as subscribers) who are meant to only see the individual posts allocated to their individual username. What's the easiest way to do this? Perhaps I could only query the posts that are in the category of the user? (I've posted each post in the category named after the customer). I hope that this clarifies my problem and what I'm trying to achieve.

Please do not hesitate to ask for more information if it isn't quite clear enough!

Thanks in advance!

  • @Aravona Not quite, I'm not trying to eliminate a certain element of the post for a user.. What I'm trying to achieve is for posts that are not 'linked' with the logged in user to not show up at all. Is there any way possible to do this? – Alex Broere Sep 1 '15 at 11:59
5

You should be able to get the current logged in user ID and then use pre_get_posts to alter the main query on the home/blog page to only show posts from that specific user. As I understand, you are talking specifically about authors.

You might want to also check the user's capabilities as well as simple subscribers won't see any posts on the blog/homepage as they can't author posts.

A very simple pre_get_posts action will look something like this: (NOTE: The following is untested)

add_action( 'pre_get_posts', function ( $q )
{    
    if ( is_user_logged_in() ) { // First check if we have a logged in user before doing anything
        if (    $q->is_home() // Only targets the main page, home page
             && $q->is_main_query() // Only targets the main query
        ) {
            // Get the current logged in user
            $current_logged_in_user = wp_get_current_user();

            // Set the logged in user ID as value to the author parameter
            $q->set( 'author', $current_logged_in_user->ID );
        }
    }    
});

EDIT

From comments, it seems that each user has a category with the same name and this specific category is then attached to a post

To accommodate this, you will need to do the following

  • As above, get the current logged in user

  • You will then need to use the info from the current user which will match up to the category. For instance and as an example, if 'display_name' == 'category name', then, if the user display name is Jane Doe, then the category name assigned to the name will also be called Jane Doe

  • In the above example, we need to get the category by name so that we can get the category ID. We will be using get_term_by() which can be used with build in taxonomies like category or custom taxonomies

  • We can then go ahead and do the same as in the original answer

You can do something like this; (I have commented the code so you can understand and follow it better)

add_action( 'pre_get_posts', function ( $q )
{    
    if ( is_user_logged_in() ) { // First check if we have a logged in user before doing anything
        if (    $q->is_home() // Only targets the main page, home page
             && $q->is_main_query() // Only targets the main query
        ) {
            // Get the current logged in user
            $current_logged_in_user = wp_get_current_user();
            /**
             * We will now get the term/category object from the user display_name
             * You will need to make sure if this corresponds with your term/category
             * If not, use the correct info to match
             */
            $term = get_term_by( 
                'name', // We will get our term by name as term name == user display_name. Change as needed
                $current_logged_in_user->display_name, // Our value to look for will be user display_name
                'category' // The taxonomy the term belongs to. category is the build in taxonomy
            );
            if ( $term ) { // Only filter the main query if we actually have a term with the desired name
                $q->set( 'cat', $term->term_id ); // Filter the posts to only show posts from the desired category
            }
        }
    }    
});
  • 1
    Please update your question properly and add all relevant info inside the question. If you want an exact answer, please provide correct details :-) – Pieter Goosen Sep 1 '15 at 15:28
  • 1
    See my update. I do not know your exact setup, so please make double sure that you match your exact setup to the code – Pieter Goosen Sep 1 '15 at 18:36
  • 1
    Any feedback on this issue? – Pieter Goosen Sep 2 '15 at 11:25
  • 1
    No problem, just curious I guess :-). This goes into functions.php (or even a custom plugin :-)) – Pieter Goosen Sep 2 '15 at 13:09
  • 1
    Sorry, I did not test the code, but there were two syntax errors which I've spotted. I have updated both code snippets and should in theory work – Pieter Goosen Sep 2 '15 at 13:56

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.