• Hi WP community –

    I’m trying to call Related Custom Posts from directly within my theme as opposed to using a Related Posts plugin, mostly because WPengine doesn’t allow most of them and most of them also don’t work with Related Posts.

    I got the following code to work to call 1 related, but I can’t figure out why it’s not looping to call 3? I’m sure I’m missing something simple, but I can’t find it. Any help?

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    <?php if (is_singular( 'team-member' ) ): 
    
    								$orig_post = $post;
    								global $post;
    								$tags = wp_get_post_tags($post->ID);
    
    								if ($tags) {
    								$tag_ids = array();
    								foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    
    								$args=array(
    								'tag__in' => $tag_ids,
    								'post__not_in' => array($post->ID),
    								'posts_per_page'=>3, // Number of related posts to display.
    								'caller_get_posts'=>1,
    								'post_type' => 'team-member',
    								'taxonomy' => 'team-member'
    								);
    
    								$my_query = new wp_query( $args );
    
    								while( $my_query->have_posts() ) {
    								$my_query->the_post(); } }
    							?>
    
    							<h3 class="related-header">Similar Partners</h3>
    								<div class="relatedthumb">
    									<a rel="external">"><?php the_post_thumbnail(array(200,200)); ?>
    									<div class="partner-name"><?php the_title(); ?></div>
    									</a>
    								</div>
    
    								<? 
    
    								$post = $orig_post;
    								wp_reset_query();
    
    								endif;
    
    ?>

    Many thanks!

    Btw, the site is https://www.getcredo.com and an example of a page where it will go is https://www.getcredo.com/team-member/john-doherty/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter dohertyjf

    (@dohertyjf)

    My friend helped me figure out. Here is the working code:

    <div class="partner-area">
    <?php if (is_singular( 'team-member' ) ):
    								$orig_post = $post;
    								global $post;
    								$tags = wp_get_post_tags($post->ID);
    
    								if ($tags) {
    								$tag_ids = array();
    								foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    
    								$args=array(
    								'tag__in' => $tag_ids,
    								'post__not_in' => array($post->ID),
    								'posts_per_page'=>3, // Number of related posts to display.
    								'caller_get_posts'=>1,
    								'post_type' => 'team-member',
    								'taxonomy' => 'team-member'
    								);
    
    								$my_query = new wp_query( $args );
    
    								while( $my_query->have_posts() ) {
    								$my_query->the_post();   ?>
    
    								<div class="relatedthumb">
    									<a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(200,200)); ?><br />
    									<div class="partner-name"><?php the_title(); ?></div>
    									</a>
    								</div>
    
    								<?php
    
    								$post = $orig_post;
    								wp_reset_query();
    
    								} }
    
    								endif;
    
    ?>
    </div>

    Now to figure out how to randomize what it calls so it’s not just calling the first 3…

    Moderator bcworkz

    (@bcworkz)

    Just add 'orderby'=>'rand', to the $args array. Mind the separating commas when you add arguments to an array.

    Thread Starter dohertyjf

    (@dohertyjf)

    Thanks @bcworks. Found that solution last night!

    Right now my code is this:

    <div class="partner-area">
    <?php if (is_singular( 'team-member' ) ):
    								$orig_post = $post;
    								global $post;
    								$tags = wp_get_post_tags($post->ID);
    
    								if ($tags) {
    								$tag_ids = array();
    								foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    
    								$args=array(
    								'tag__in' => $tag_ids,
    								'post__not_in' => array($post->ID),
    								'posts_per_page'=>3, // Number of related posts to display.
    								'caller_get_posts'=>1,
    								'post_type' => 'team-member',
    								'taxonomy' => 'team-member',
    								'orderby' => 'rand'
    								);
    
    								$my_query = new wp_query( $args );
    
    								while( $my_query->have_posts() ) {
    								$my_query->the_post();   ?>
    
    								<div class="relatedthumb">
    								<a rel="external" href="<? the_permalink()?>">
    									<div class="related-image"><?php the_post_thumbnail(array(200,200)); ?></div>
    									<br />
    									<div class="partner-name"><?php the_title(); ?></div>
    									</a>
    								</div>
    
    								<?php
    
    								$post = $orig_post;
    								wp_reset_query();
    
    								} }
    
    								endif;
    
    ?>

    It’s working pretty well! Now just buttoning up some CSS.

    Thread Starter dohertyjf

    (@dohertyjf)

    So here’s another question – how would I make this code grab the first tag and match based off of that? I’m still only getting three or four different related “posts” pulling and not sure why.

    Moderator bcworkz

    (@bcworkz)

    Change the first query arg to this:
    'tag__in' => array( $tag_ids[0]),

    This could reduce the posts returned though, only posts with the one tag instead of posts with any of multiple tags (assuming there are multiple tags)

    The ‘posts_per_page’ argument controls the posts returned, so you should get at most 3 results for any query. You could set this to -1 temporarily to see all posts in the pool from which ‘rand’ pulls from.

    You may as well change ‘caller_get_posts’ to ‘ignore_sticky_posts’. It won’t change things, but ‘caller_get_posts’ has been deprecated (but still works). Also try removing 'taxonomy' => 'team-member',. I don’t think it’s doing anything, unless you have a taxonomy whose slug is actually ‘taxonomy’. In that case it could be further restricting posts returned.

    Thread Starter dohertyjf

    (@dohertyjf)

    Thanks @bcworkz. Didn’t really do anything unfortuntely. Changing it to grab just one tag actually further restricted.

    An example page is https://www.getcredo.com/team-member/john-doherty/. If you click around between the different profiles, you’ll often just see the same three. But, https://www.getcredo.com/team-member/greg-gifford/ returns better since he just has 1 tag. Thoughts?

    Moderator bcworkz

    (@bcworkz)

    I’m not sure how to explain the behavior. It may have to do with there only being a couple popular tags and how SQL orders the results prior to randomizing, combined with how the randomizing actually works. I don’t trust how truly random the ‘rand’ orderby function is.

    I would try setting ‘posts_per_page’ to -1 and removing the ‘rand’ orderby just to satisfy yourself that the query is working with the proper pool of team members. If that checks out then the ‘rand’ function just isn’t very random.

    If that’s the case, you could try randomizing with PHP instead. Query all related team members, then generate 3 random integers within the range of array indexes and only output those three members. Not very efficient, but OK for any reasonable size of results. The ‘rand’ SQL function is horribly inefficient anyway.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Help for Related Posts within Custom Post Type? Not looping’ is closed to new replies.