0

I have a custom post type setup called 'Blog' and I have added the ability to assign authors to posts.

I want to be able to display a list of posts made by an author, however the posts never appear for the custom post type, even though posts have been made by the author the loop always displays: 'No posts by this author.'

Here is the loop in my author.php file:

<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
<h1>Author: <?php echo $curauth->first_name; ?> <?php echo $curauth->last_name; ?></h1>

<!-- The Loop -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    $postThumb = get_field( 'news_image' , $post->ID );
    $postThumb = $postThumb['sizes']['insightsPostThumb'];

    $cNewsBlogItem = array(
       'img'    => $postThumb,
       'title'  => $post->post_title,
       'date'   => date( 'd F, Y', strtotime( $post->post_date ) ),
                'link'  => get_permalink( $post->ID )
    );
?>
<?php
    if(!$cNewsBlogItem['img'] || $cNewsBlogItem['img'] == ""){
        // if no img use fallback
        $cNewsBlogItem['img'] = get_bloginfo('template_directory').'/images/news-fallback.jpg';
    }
?>

<a class="cNewsBlogItem <?php echo $cNewsBlogItem['class']; ?>" href="
<?php echo $cNewsBlogItem['link']; ?>" title="<?php echo $cNewsBlogItem['title']; ?>">

<img src="<?php echo $cNewsBlogItem['img']; ?>" alt="<?php echo $cNewsBlogItem['title']; ?>" />

  • Updated my question with some code at the top of the page that gets author information. – Neelam Khan Jun 19 '17 at 11:30
0

I managed to resolve this issue by using pre_get_posts function and adding the custom post type to the main query.

I added the below to my functions.php file and now I get results for authors in my custom post type:

/* pre_get_posts function added to include post type blog in author loop */
  function add_cpt_author( $query ) {
    if ( !is_admin() && $query->is_author() && $query->is_main_query() ) {
    $query->set( 'post_type', array('post', 'blog' ) );
    }
  }
  add_action( 'pre_get_posts', 'add_cpt_author' ); 

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.