• Resolved Auror

    (@auror)


    Hi! I’m running into this problem with a custom page I’ve set up to list authors according to a custom field (using Advanced Custom Fields).
    Even though the query works fine (that is, the results are those I’m expecting), I simply can’t get the URL for the author archive page to work. Here’s my code:

    <?php
    
    /**
    * Template Name: Authors list
    * @package Educaz
    */
    
     get_header(); ?>
    
    <div id="primary" class="content-area grid__col grid__col--2-of-3">
    	<main id="main" class="site-main" role="main">
    
        <?php
    
        // wp_user_query arguments
        $argsProf =array(
          'order'      => 'ASC',
          'orderby'    => 'ID',
          'meta_key'   => 'academic_level',
          'meta_value' => 'professor'
        );
    
        //create wp_user_query object
        $queryProf = new wp_user_query($argsProf);
        //get the results
        $professors = $queryProf->get_results();
    
        //check for professors
        if (! empty($professors)) {
          echo '<ul>';
          foreach ($professors as $professor) {
            $professor_info = get_userdata($professor->ID);
            echo '<li><a href="'  .  <strong>esc_url(get_author_posts_url($professor_info->ID, $professor_info->user_nicename))</strong> . '">' . $professor_info->display_name . '</a></li>';
          }
          echo '</ul>';
        }
        ?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php
    get_sidebar();
    get_footer();

    The code works fine if don’t call get_header, but then, of course, the theme doesn’t load properly on the page.
    Two things I’ve tried also didn’t work:
    1. Someone suggested I shouldn’t do a wp_user_query using custom fields as meta_key. I then changed the query to a simpler one, using ‘search’ as a parameter. No luck. (Again, the query in the code above seems to work fine, since it retrieves the results I’m expecting)
    2. I’ve also tried hardcoding an ID in get_author_posts_url. That also doesn’t work, surprisingly. I’ve even tried echoing the get_author_posts_url, but I get nothing.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    When you call get_header(), it fires the wp_head action which all sorts of plugins and themes use to initialize something they need for the output. One of those modules is creating some sort of conflict with get_author_posts_url(), as it should always work when fed a proper ID.

    You can try deactivating various modules to discover which is causing a conflict with get_author_posts_url(), or work around it by building the URL yourself based on $professor_info->user_nicename. The correct URL depends on your site settings, but it’s often site_url("/author/{$professor_info->user_nicename}/")

    Thread Starter Auror

    (@auror)

    Wow, you were completely right. It worked. I deactivated all plugins, and it worked. I then re-activated three or four at a time to narrow down the culprit. It was a plugin called Show Hide Author. I’ll try and contact the author of that plugin. Many thanks!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. I guess that plugin was doing its job a little too well on the hiding end ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘problem with get_author_posts_url’ is closed to new replies.