i want to have different homepage according the user role login

if the user is not log they have a landing page with login

if a candidate is log the home page is custom for candidate

if a employer is log the home page is custom for employer

i have create my home page directly in wordpress not in code with page.php

and I want to have the same url for 3 home page

so I write in other question here : Dynamic homepage according to user role

I try that :

   function wpse_273872_pre_get_posts( $query ) {
  if ( $query->is_home() && $query->is_main_query() ) {
    if(!is_user_logged_in()) return;

    $current_user = wp_get_current_user();
    $user = new WP_User( $current_user->ID);

    if(in_array('candidate', $user->roles)){ //assuming the role name is candidate
      $query->set('p', [ID of the page you created for candidate]);
      $query->set('post_type', 'page');
    elseif(in_array('company', $user->roles)){ //assuming the role name is company
      $query->set('p', [ID of the page you created for employer]);
      $query->set('post_type', 'page');
add_action( 'pre_get_posts', 'wpse_273872_pre_get_posts' );

but he doesn't work I dont now why

any solution ?



$query->is_home() is not, well, 'home'. $query->is_home() checks of you are viewing the blog/posts page. It's an artifact from when the home page of WordPress sites was invariably the blog. You want to change that to $query->is_front_page(), which checks if the front page of the website is a page, and if you are on that page.

  • ok and it's already in function.php ?
    – ouanounou
    Jul 20 '17 at 9:57
  • I don't understand the question. The code can be in functions.php or a plugin. All you need to is change the bit I mentioned, and replace the "[ID of the page you created for employer]" bits with the action ID. Jul 20 '17 at 10:05
  • this function who have code I put in function.php
    – ouanounou
    Jul 20 '17 at 10:05
  • but I try and now my website is down blank page
    – ouanounou
    Jul 20 '17 at 10:05
  • You've left in the placeholder [ID of the page you created for employer] from the previous answer. You need to replace that with the actual ID. Jul 20 '17 at 10:06

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.