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 isJane Doe
, then the category name assigned to the name will also be calledJane 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 likecategory
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
}
}
}
});