0

I am creating a custom author.php page and on that page I want to have different areas where I can display custom post types (cpt). So in one area I will display the cpt 'blogs', in another area the cpt 'audio' and in another area cpt 'video', etc. -- but all for one author -- say by Albert Einstein, and on his author page.

Ideally, each loop would be conditional so that when there was no cpt, say for 'video' or 'blogs', nothing would display.

I have looked around but cannot find code that works for me. If anyone can help provide code for looping at least one of these cpt's that would be very helpful. Thank you! Here is the code that I am using -- for some reason it is not calling the ctp so no posts are appearing on the page:

<?php
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
    query_posts( array(
            'post_type' => array('video'),
            'author' => $author,
            'posts_per_page' => 10 )
        );
?>
  • Could you post here your current code? What's wrong with it? – Krzysiek Dróżdż Nov 23 '13 at 22:02
  • @KrzysiekDróżdż Sure! I just edited my question above to include the code. Any advice is greatly appreciated. Thanks! – user2591811 Nov 23 '13 at 22:11
1

You should not be using query_posts() to create custom queries, for this, use WP_query()

You should get what you want with this code.

    <?php
    $args = array(
        'post_type'      => array( 'post' ),
        'posts_per_page' => 10,
        'author'         => $author
    );


    $author_videos = new WP_Query( $args );

    if ( $author_videos->have_posts() ) : while ( $author_videos->have_posts() ) : $author_videos->the_post(); ?>
            <h1><?php the_title(); ?></h1>
        <?php endwhile;
            wp_reset_postdata();
endif; ?>
  • Why an empty else? – s_ha_dum Nov 23 '13 at 23:08
  • Hi @Marcos, I had just read about using WP_query() instead of query_posts() so thanks for correcting me! I tried your code, changing the post_type to 'video' but it is not working for some reason. The page comes up white/blank. I am currently trying to access my error logs to see which line of code it is... – user2591811 Nov 23 '13 at 23:15
  • @s_ha_dum it's just an example in case he would use it. It's easier to delete than write it. ;) – Marcos Rodrigues Nov 23 '13 at 23:24
  • Are you using this conde on the authors.php file? – Marcos Rodrigues Nov 23 '13 at 23:25
  • 1
    @MarcosRodrigues : welcome. You seem to be new here so I am thinking you don't realize how likely it is that that your code will copied and pasted into who knows how many sites more or less blindly :) It is best to spell out all the caveats explicitly. – s_ha_dum Nov 23 '13 at 23:27

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.