2

I am trying to modify my author.php page so that it includes the custom post types which the author has added.

The below is what I have where it first displays the normal posts, and then displays the custom post types (my_custom_post_type).

The first part seems to work fine and it displays correctly. However the second part seems to display ALL the custom post types, and not just those which relate to the author of the author.php page.

Any help would be super appreciated. Thank you!

<!-- Show normal posts from the author -->
<?php if ( have_posts() ): ?>
    <h3>Posts by <?php echo $curauth->first_name; ?>:</h3>
    <?php while ( have_posts() ) : the_post(); ?>
        <p><?php the_title(); ?></p>
    <?php endwhile; ?>
<?php else: ?>
    <p><?php _e('User has no posts'); ?></p>
<?php endif; ?>


<!-- Show custom post type posts from the author -->
<?php global $wp_query;
query_posts( array(
    'post_type' => 'my_custom_post_type' ,
    'author=' . $authorid,
    'showposts' => 10 )
); ?>

<?php if ( have_posts() ): ?>
    <h3>Custom post entries by <?php echo $curauth->first_name; ?>:</h3>
    <?php while ( have_posts() ) : the_post(); ?>
        <p><?php the_title(); ?></p>
    <?php endwhile; ?>
<?php else: ?>
    <p><?php _e('User has no custom posts'); ?></p>
<?php endif; ?>
5

I'll guess that you don't set $authorid anywhere, so it's being ignored and returning all posts regardless of author. If you enable debugging, you'll get a warning telling you that $authorid is undefined. It's good practice to always develop with debugging enabled, so you don't have to guess what your errors are.

Also, don't use query_posts for additional queries, or ever actually! It overwrites the original main query, and may produce unpredictable results elsewhere in your template. Use WP_Queryinstead:

$args = array(
    'post_type' => 'my_custom_post_type' ,
    'author' => get_queried_object_id(), // this will be the author ID on the author page
    'showposts' => 10
);
$custom_posts = new WP_Query( $args );
if ( $custom_posts->have_posts() ):
    while ( $custom_posts->have_posts() ) : $custom_posts->the_post();
        // your markup
    endwhile;
else:
    // nothing found
endif;

Note in the case of a custom query, we assign posts to $custom_posts (or whatever unique variable name you'd like to use), and then reference that within the loop.

0

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.