• I wanted to add a “Posts by same author(s)” at the end of my articles. Since I’ve been struggling with this for days and found bits of answers here and there, I thought I could share this so it might help others now that it’s working for me.

    <div class="relatedposts">
    <?php
    $orig_post = $post;
    global $post;  
    
    $post_co_authors = get_coauthors( $post->ID );
    // build the slugs array
    $ca = array();
    foreach ($post_co_authors as $c) {
      array_push($ca,'cap-'.$c->user_nicename);
    }
    $args = array(
      'post_type' => 'post'
      ,'post__not_in' => array($orig_post->ID)
      ,'posts_per_page' => -1
      ,'post_status' => 'publish'
      ,'tax_query' => array(
        array(
          'taxonomy' => 'author'
          ,'field' => 'slug'
          ,'terms' => $ca
        )
      )
    );
    $q = new wp_query( $args );
    if ( $q->have_posts() ) {
      ?>
      <h4>From same author(s)</h4>
      <ul>
      <?php
      while ( $q->have_posts() ) {
        $q->the_post();
        if ($post->ID != $orig_post->ID) {
        ?>
        <li><a rel="external" href="<? the_permalink() ?>"><? the_title(); ?></a></li>
        <?php
      }
    }
    ?>
    </ul>
    <?php
    }
    $post = $orig_post;
    wp_reset_query();
    ?>
    </div>

The topic ‘[plugin: co_authors-plus] Example of "Related posts"’ is closed to new replies.