include posts and custom posts on 'team' custom post page

Hi,

I have recently built http://blueventures.org and I need some help with some of the final dynamic elements of the site.

For each team member, for example http://blueventures.org/team/laura-robson/ I would like to be able to show that users standard WP posts from the ‘In the media’ category and their ‘Publications’ which is a custom post type.

Ideally I would like to include the featured image and date but to start with the title with link to the story/publication would be great.

Ideally these sections would only show if there are stories/publications by the author too as many of the ‘team’ wont have any.

Eventually I would also like to be able to show their blogs too but they are on a subsite (multisite) so I’m guessing more complicated again! Perhaps using the global recent posts plugin or similar?

There is a custom single.php for the post type single.tshowcase.php which I’ve pastebinned here http://pastebin.com/Y6Rx5xc0

Really hope you can help!

Thanks,

Martin

  • Patrick
    • Support Monkey

    Hi there @martin

    I hope you’re having a great day!

    Likely the easiest way to do that is by leveraging the pre_get_posts hook as detailed in the codex here:

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    You could instead alter the query directly in your template with query_posts as seen in this example:

    https://wordpress.org/support/topic/query-multiple-custom-post-types-in-single-loop#post-3047083

    Please don’t hesitate to post again if you need more help with this.

  • Martin
    • Flash Drive

    Thanks @patrick for the pointers… this is a whole new level for me!

    So, following the second method you suggested, if I did something like:

    <?php query_posts( array(
    'post_type' => array( 'post', 'publication'),
    'cat' => 3,
    'showposts' => 3 )
    ); ?>
    <?php while ( have_posts() ) : the_post(); ?>

    <div class="post_type_media">
    <?php if ( 'post' == get_post_type() ) : ?><h3>In the Media</h3><?php endif; ?>
    <a href="<?php the_permalink(); ?> "><?php the_title(); ?></a>
    </div>
    <?php endwhile; ?>

    <?php query_posts( array(
    'post_type' => array( 'publication'),
    'showposts' => 3 )
    ); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <div class="post_type_media">
    <?php if ( 'publication' == get_post_type() ) : ?><h3>Publications</h3><?php endif; ?>
    <a href="<?php the_permalink(); ?> "><?php the_title(); ?></a>
    </div>
    <?php endwhile; ?>

    Would that work?

  • Patrick
    • Support Monkey

    Hi again @martin

    That’s an excellent start. But if I’m reading that right, it looks like you want to separate the post-types into 2 distinct sections, is that correct?

    If you are putting 2 post loops (sounds like a breakfast cereal!) on the same page, you’ll need to “reset” the loop so it doesn’t affect other loops on your site.

    Here’s a great little article on the 3 different types of “resets” you can use depending on the type of loop and where it is located:

    http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/

    You’re gonna want to use wp_reset_postdata() and structure your multiple loops as in the example given in the article so they don’t conflict or blow up. :slight_smile:

  • Martin
    • Flash Drive

    Ok cool, thanks.

    So I add in the wp_reset_postdata and end up with something like this

    //Adding posts and publications to team member body text
    <?php query_posts( array(
    'post_type' => array( 'post'),
    'cat' => 3,
    'showposts' => 3 )
    ); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <div class="post_type_media">
    <?php if ( 'post' == get_post_type() ) : ?><h3>In the Media</h3><?php endif; ?>
    <a href="<?php the_permalink(); ?> "><?php the_title(); ?></a>
    </div>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

    <?php query_posts( array(
    'post_type' => array( 'publication'),
    'showposts' => 3 )
    ); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <div class="post_type_pub">
    <?php if ( 'publication' == get_post_type() ) : ?><h3>Publications</h3><?php endif; ?>
    <a href="<?php the_permalink(); ?> "><?php the_title(); ?></a>
    </div>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    //End adding posts and publications

    Is it as simple as that? Do I need to add any other code? I tried adding it at various points on the single-tshowcase.php (http://pastebin.com/Y6Rx5xc0) but just ended up with a white screen. Any advice where/how I should be looking to add it?

    Thank you!

  • Ash
    • WordPress Hacker

    Hello @martin

    I hope you are well today.

    Is publication post type in the same site where you want to get ‘post’ post type? If yes, and whenever you get a white screen, please enable debug mode. It will show the error type in the browser.

    So, would you please enable debug mode and check if any error is shown in that page?

    To enable debug mode please edit a line in your wp-config.php:

    Change

    define('WP_DEBUG', false);

    to

    define('WP_DEBUG', true);

    Please let us know what errors you see there.

    Cheers

    Ash

  • Predrag Dubajic
    • Support

    Hey Martin,

    Can you paste your custom code in the file like you did when you got the error and paste that whole page in pastebin.

    Now enable wp_debug and see what warning is displayed.

    Paste both the pastebin link and error message here as this will help us a lot to pinpoint the issue.

    Best regards,

    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.