I'm looking for the best way to go about creating, probably, custom post type where you can filter in the categories related to that custom post type in the sidebar.

Here is the website I'm working on. Originally the student and donor profiles should be some type of post or category that can dynamically be updated when a new profile is created. So Health & Science, Student Success and Creative community have their own set of specific profiles.

So lets say John Doe is a new student profile for Student Success I can tag him to feed through that page alone and when viewing his individual post I'm having trouble displaying the categories or profiles that are related, in this case Student Success. So when viewing John Doe's profile the other profiles under student success should be listed on the sidebar.

I noticed wordpress shares the single.php template file so the sidebar I have for the single.php file is being used for the profiles which we don't want.

Overall, if you run through Health & Science, Student Success and Creative community I'm looking for a set up like that but instead of a 4 step process that the client has to do when uploading a new profile I am looking for just a one step process.

My current set up is you add the new profile as a page then go to the menus area in the dashboard and add the new profile to the menu.

My current page template code.

  • So basically what you want, is for the sidebar that's in single.php to detect the post being pulled in the page template and use a category or post type to determine what related profiles/posts to show? So right now Health & Science, Student Success and Creative community are just pages with templates versus a custom post type?
    – RachieVee
    Jun 26, 2014 at 15:08
  • Yes. I been looking into the conditional statements that wordpress uses but I can't figure how I can get it to read that page and show the related post.
    – jose
    Jun 26, 2014 at 17:15
  • Ok give me a bit of time to try and imitate what you want in a WordPress install of mine and I'll see what I can provide.
    – RachieVee
    Jun 27, 2014 at 14:32
  • I appreciate your help @RachieVee. If it starts to become to much trouble you don't have to worry about it. Thanks!
    – jose
    Jun 27, 2014 at 15:03
  • Oh man, haha, yeah it's taking me a while, but if you don't mind that I don't provide code, I can at least try and point you in the right direction. I see in your template that you're querying posts with a category - why not create a new loop in your Donor Profiles area that gets posts by that category and lists out the links (assuming the category is something like Health-Science or one of your three sections)? Or you can use post meta in your profiles if you don't want to pull a category for a menu created with a get_posts loop. I feel like I didn't make sense? Let me know.
    – RachieVee
    Jun 27, 2014 at 15:21

2 Answers 2


$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5,'post_type' => 'news', 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
        <?php  the_post_thumbnail(); ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>


<?php }
wp_reset_postdata(); ?>
  • 3
    How does this answer the question?
    – Rarst
    Sep 9, 2014 at 6:56
  • This did work for me..
    – rwzdoorn
    Jul 15, 2015 at 11:42

First you must make a query with a custom taxonomy or anything you would like to show your posts

  function related_posts_by_taxonomy( $post_id, $taxonomy, $args=array() ) {
        $query = new WP_Query();
        $terms = wp_get_object_terms( $post_id, $taxonomy );
        // Make sure we have terms from the current post
        if ( count( $terms ) ) {
            $post_ids = get_objects_in_term( $terms[0]->term_id, $taxonomy );
            $post = get_post( $post_id );
            $post_type = get_post_type( $post );        
            $args = wp_parse_args( $args, array(
                    'post_type' => $type,
                    'post__in' => $post_ids,
                    'taxonomy' => $taxonomy,
                    'term' => $terms[0]->slug,
                ) );
            $query = new WP_Query( $args );
        // Return our results in query form
        return $query;

To list the output you can use the code below

    $related =  related_posts_by_taxonomy( $post->ID, 'service_category' );
                    while ( $related->have_posts() ): $related->the_post(); ?>
                    <li class="folk-grid__col all">
                                <img class="" data-src="<?php echo get_template_directory_uri(); ?>/images/icons-service/<?php $icon_service = get_post_meta( $post->ID, 'service_options_icons', true ); echo $icon_service; ?>.svg" alt="<?php echo get_the_title( $post->ID ); ?>" src="<?php echo get_template_directory_uri(); ?>/images/icons-service/<?php echo $icon_service; ?>.svg">
                                   <span><a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>"><?php echo get_the_title( $post->ID ); ?></a></span>
                                       <a rel="nofollow" class="btn folk-button" style="top: 35%;width: 62%;padding: 10px;" href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>">Enquire now</a>

Not the answer you're looking for? Browse other questions tagged or ask your own question.