I'm trying to create my own user profile pages. When someone requests example.com/profile/username, I want to query the user tables for a user with username (= slug) and display the result.

Ideally, I would alter the main query to query only the user tables and return this result. This does not seem to be possible (WP_Query and WP_User_Query are totally separate). Correct?

I do not want to:

  • redirect to a page or anything else
  • hack my templates
  • insert a post/page for each user
  • install any plugins

Current Idea

It seems the only way to avoid redirects and let my templates work for real 404's is with an action hook at parse_query that sets is_404 to false when my profile route is matched (via REQUEST_URI and preg_match).

To avoid hacking my templates, I'd like to perform my user query ASAP and add the result to the main query. I am thinking of performing my user query and simply turning the result into a post on the fly and letting that be the sole result of the main query. This seems the cleanest way to work views into Wordpress. However, I do not want to insert this post into my tables.

Can I create a post and pass it to a template as a main query result without saving it elsewhere, or would I need to insert and then delete it? Will waiting until parse_query cause problems?

Is there a better way?


From what I understand you are trying to change the permalink structure of the user, so instead of showing yoursite.com/author/yourusername it will show yoursite/profile/yourusername?

You can add a rewrite rule to the permalink structure to make that change:

function yourdomain_user_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->author_base = 'profile';
    $wp_rewrite->author_structure = '/' . $wp_rewrite->author_base . '/%author%';

add_action( 'init', 'yourdomain_user_rewrite' );

Then Worpdress will automatically load author.php on your theme when yoursite/profile/XXXX is queried. No complicated queries involved!

Is that what you are looking for?

  • These are mostly profiles of subscribers, not authors, and I am using only index.php. I very, very simply want to parse the URI into a query of my user tables and pass the query results to my template. That is all. I don't want to redirect or rewrite or parse the URI any other way. I don't want to attempt or run any other queries. There appears to be no way to stop Wordpress from trying to get a post. There is no post. It's a view of user data. Anyway, it's not important for me anymore. I have left WP for an MVC architecture.
    – Rachel
    Jan 19 '17 at 22:06
  • Hi Rachel. Wordpress is a bit quirky, but very capable. One of its biggest tricks is the Template Hierarchy which matches the type of information to layouts on your theme (which is kind of like a controller). So by having an author.php file in your theme, it will be used for any user; then you can have author-{id}.php for specific users etc. Sorry we couldn't help this time, but hope you work it out! Jan 20 '17 at 10:39
  • No worries. I want to do too many things differently, but maybe the answer will help someone else. To me, WP does templating backwards. You don't conditionally send requests to different templates and then include in each the parts that are the same. You send all requests to the same template and then conditionally include the parts that are different. But so you're saying I can add a rewrite rule for author pages and then check is_author() and render what I want based on that? And permalinks and the info bar will work correctly?
    – Rachel
    Jan 20 '17 at 16:52

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.