Combining the author and post_type in an array
-
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)
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.