• I’m running a foreach loop inside the WordPress loop to give me a list of Custom Post Types written by the author of the post in question.

    This is the code I’m working with at the moment.

    <?php
    global $authordata, $post;
    $author_id = $authordata->ID;
    $cpt_id = 'featured';
    $args = array( 'numberposts' => 5, 'author' => $author_id );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; wp_reset_postdata(); ?>
    </ul>

    The above shows me a list of the author’s last 5 posts without the CPT defined in the array.

    <?php
    global $authordata, $post;
    $author_id = $authordata->ID;
    $cpt_id = 'featured';
    $args = array( 'post_type' => $cpt_id, 'numberposts' => 5 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; wp_reset_postdata(); ?>
    </ul>

    The above shows me a list of the Custom Post Type in question as the author array has not been included.

    <?php
    global $authordata, $post;
    $author_id = $authordata->ID;
    $cpt_id = 'featured';
    $args = array( 'post_type' => $cpt_id, 'numberposts' => 5, 'author' => $author_id );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; wp_reset_postdata(); ?>
    </ul>

    But when combining the two the result suddenly becomes blank. Am I missing something simple or is what I desire not possible. Thanks for looking at my code.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Did you ever get an answer for this issue? I have been struggling with the same thing.

    Thread Starter theApe

    (@theape)

    Not specifically, what are you trying to do. I accomplished what I was after in a round about way. Works for my needs, but was complicated. As I said, can you be specific about what you’re trying to do, I might be able to help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Combining the author and post_type in an array’ is closed to new replies.