1

I am trying to create a profile page for every user if his / her role is a custom role.

Ive created a custom role in my site. Now this is the default role is assigned to the members when they register on the site.

I have contributors.php and authors.php. If I go to www.mysite.com/authorsit lists all the users without considering the role. Then I used Author Base using generate_rewrite_rules to rewrite this URL.

Now if I go to www.mysite.com/mycustomrole it lists all the users. It's fine for me.

Next step to create a profile page for each members. So I've created author.php

When I have the users below,

  • user1 - administrator
  • user2 - custom role
  • user3 - administrator
  • user4 - custom role

I am able to see thier profile page like this.

  • If I go to www.mysite.com/authors/user1 it shows his profile.
  • If I go to www.mysite.com/authors/user2 it does not show his profile.
  • If I go to www.mysite.com/authors/user3 it shows his profile.
  • If I go to www.mysite.com/authors/user4 it does not show his profile.

So I am getting individule profile page only for administrator role user and not for my custom created role users. Why is that.

For now my requirements are:

    1. I want to get the URL for an individual custom user like : www.mysite.com/customrole/user1
    1. I want to show the custom role users only under that URL. but it's fine if I am able to show other role users too under that url.

How can I do this WOTHOUT using any plugins?

7
  • In my humble opinion, this is an excellent question. Can you please provide just a little more detail though? You say that for user1 and user3 "it does not show his profile." Exactly what does that mean? Is the template author.php still being displayed, but with incorrect information, or are you recieving a 404 error?
    – David Gard
    Jan 26, 2015 at 12:06
  • hi @DavidGard yesterday it did show 404. but today its shows just a blank page. I f see the source code, source code also just blank.. do not know what happened or why is this.. Jan 26, 2015 at 12:23
  • If your custom users are not showing up, you might try grabbing some of the globals (e.g. wp_query, post ) and dumping them at the top of the page to see how they are configured ... or hooking into the template_include hook or pre_get_posts hooks to see what is going on just before the template is loaded.
    – Privateer
    Jan 26, 2015 at 13:03
  • on their posts I am getting their details. eg. I see the author of the post in the front end even the author is a custom role user. Issue with their profile page only. www.mysite.com/authors/user2 If I go to this page.. It just shows a blank page... Jan 26, 2015 at 16:30
  • @Privateer and david: I've followed this tutorial: wpbeginner.com/wp-themes/… Now it shows all the posts by the admin role user. It shows 8 post for user1. Because he is an admin and he posted 8 posts. That's fine. The issue is it says No Posts By This author for user1 and user2 (for www.mysite.com/authors/user2 and www.mysite.com/authors/user4) But one post was posted by each of this custom role users. and Note those posts also custom post types suppose is this the reason? Jan 26, 2015 at 17:10

1 Answer 1

1

You noted that you are using custom post types.

To see if that is the problem:

  1. Create a normal "post" for each user (not a custom post type)
  2. See if their pages suddenly show up.

If so, then more than likely your author page is not set up to show show custom posts.

To fix that, you might use something like the following in your funtions.php file (or alter the query on the authors page):

function my_show_special_posts_on_author( WP_Query $query ) {
    # Make sure you are only altering the query on the author page
    if ( $query->is_author() && $query->is_main_query() && !is_admin() ) {
        # Grab the current post types to be shown
        $types_to_show = $query->get('post_type');
        $types_to_add = array( 'custom_post_type_1', 'custom_post_type_2' );
        if ( is_array($types_to_show) ) {
            # Already showing an array of types, add yours if not already included
            foreach ( $types_to_add as $post_type ) {
               if ( !in_array($post_type, $types_to_show) ) {
                  $types_to_show[] = $post_type;
               }
            }
        } else if ( empty($types_to_show) ) {
            # Strange. Not showing any types. Add yours anywise.
            $types_to_show = $types_to_add;
        } else {
            # A single one as a string, add it to your types to add then overwrite types to show
            $types_to_add[] = $types_to_show;
            $types_to_show = $types_to_add;
        }
        $query->set('post_type', $types_to_show);
    }
}
add_action('pre_get_posts', 'my_show_special_posts_on_author');

Be sure to adjust the line $types_to_add = array( ... );

That should force your custom post types to show.

2
  • Thanks for that Privateer.. on my author page ( www.mysite.com/authors/user2 ) I am getting the custom posts of the custom role user. That's fine. Actually I want to get that URL like this www.mysite.com/customrole/user2. How can I do that. Jan 27, 2015 at 4:25
  • Very welcome. To change the link structure, you would need to filter the permalinks and re-write the authors permalink (maybe by pulling their first role) You might search here for how to do that or ask another question as I'm not so familiar with doing that at the moment.
    – Privateer
    Jan 27, 2015 at 14:22

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.