Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Related posts within a custom post type. WordPress

  • SOLVED

I'm looking for some code to display a random selection of 21 related posts in a custom post type. I'd like them to be displayed as an ordered list.

Answers (3)

2012-05-25

Arnav Joy answers:

try this
<?php
wp_reset_query();
$custom_post_type = 'yourposttype';
$tags = wp_get_post_tags(get_the_ID());

if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>-2,
'post_type' => $custom_post_type,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h2>Related Products</h2>';
echo '<ul';
while ($my_query->have_posts()) : $my_query->the_post();
if ( has_post_thumbnail()) :
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'product-thumb-1');
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
<?php endif; ?>
<?php endwhile; } wp_reset_query();
echo '</ul>';
} ?>


Arnav Joy comments:

ignore previous code

<?php

// try this replace "yourposttype" with the post type you have
wp_reset_query();
$custom_post_type = 'yourposttype';
$tags = wp_get_post_tags(get_the_ID());

if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>21,
'post_type' => $custom_post_type,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h2>Related Products</h2>';
echo '<ul';
while ($my_query->have_posts()) : $my_query->the_post();
if ( has_post_thumbnail()) :
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'product-thumb-1');
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
<?php endif; ?>
<?php endwhile; } wp_reset_query();
echo '</ul>';
} ?>


Scott Hack comments:

Code below isn't displaying anything. Thoughts on adding an else statement to the if statement at least for debugging so we can see if it is pulling any posts?


Arnav Joy comments:

<?php


//try this
wp_reset_query();
$custom_post_type = 'yourposttype';
$tags = wp_get_post_tags(get_the_ID());

if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
//'post__not_in' => array(get_the_ID()),
'showposts'=>21,
'post_type' => $custom_post_type,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h2>Related Posts</h2>';
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
<?php endif; ?>
<?php endwhile; } wp_reset_query();
echo '</ul>';
}

else { echo 'You have not added tags in post please add it.'; }
?>


Scott Hack comments:

None of the posts have tags. I only want to pull from other posts that are in the custom post type.


Arnav Joy comments:

<?php

//try this
wp_reset_query();
$custom_post_type = 'post';

$args = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '21' ,'post_type' => $custom_post_type ) );
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h2>Related Posts</h2>';
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>

<?php endwhile; } wp_reset_query();
echo '</ul>';

?>


Arnav Joy comments:

do not forget to edit line
$custom_post_type = 'post';

with your post type


Scott Hack comments:

This query doesn't seem to be pulling in any posts, all it is displaying is the </ul> in the code.


Arnav Joy comments:

can you share the full code where you are using my code?



Arnav Joy comments:

try this

<?php

//try this
wp_reset_query();
$custom_post_type = 'mls';

$args = array ( 'orderby' => 'rand', 'posts_per_page' => '21' ,'post_type' => $custom_post_type );
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h2>Related Posts</h2>';
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>

<?php endwhile; } wp_reset_query();
echo '</ul>';

?>


Arnav Joy comments:

<?php

//try this
wp_reset_query();
$custom_post_type = 'mls';

$args = array ( 'orderby' => 'rand', 'posts_per_page' => '21' ,'post_type' => $custom_post_type );
query_posts($args);
if( have_posts() ) {
echo '<h2>Related Posts</h2>';
echo '<ul>';
while (have_posts()) : the_post();
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>

<?php endwhile; echo '</ul>';} else { echo 'No post'; }
wp_reset_query();
?>


Arnav Joy comments:

use following line after the $custom_post_type = 'mls';

remove_all_filters('posts_orderby');


Scott Hack comments:

THAT WORKED!!!!

2012-05-25

Daniel Yoen answers:

in functions.php

function related_post()
{
$backup = $post; // backup the current object
$postID = get_the_ID();
$tags = wp_get_post_tags($post->ID);

if ($tags)
{
echo '<h3>Related Posts</h3>';
$first_tag = $tags[0]->term_id;
$args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($postID), 'showposts'=> 8, 'post_type' => 'your post type here', 'caller_get_posts'=> 1 );
$my_query = new WP_Query($args);

if($my_query->have_posts())
{
echo '<ul>';

while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="alternate" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

<?php endwhile;
echo '</ul>';

}
}

$post = $backup; // copy it back
wp_reset_query(); // to use the original query again

}

in single.php

<?php related_post(); ?>

hope this help :)


Scott Hack comments:

This didn't do anything.


Daniel Yoen comments:

function related_post()
{
$backup = $post; // backup the current object
$postID = get_the_ID();

if ($tags)
{
echo '<h3>Related Posts</h3>';
$first_tag = $tags[0]->term_id;
$args=array( 'post__not_in' => array($postID), 'showposts'=> 8, 'post_type' => 'your post type here', 'caller_get_posts'=> 1 );
$my_query = new WP_Query($args);

if($my_query->have_posts())
{
echo '<ul>';

while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="alternate" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

<?php endwhile;
echo '</ul>';

}
}

$post = $backup; // copy it back
wp_reset_query(); // to use the original query again

}

Good luck :)


Daniel Yoen comments:

function related_post()
{
$backup = $post; // backup the current object
$postID = get_the_ID();

if ($tags)
{
echo '<h3>Related Posts</h3>';
$first_tag = $tags[0]->term_id;
$args=array( 'post__not_in' => array($postID), 'showposts'=> 8, 'post_type' => '<strong>your post type here</strong>', 'caller_get_posts'=> 1 );
$my_query = new WP_Query($args);

if($my_query->have_posts())
{
echo '<ul>';

while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="alternate" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

<?php endwhile;
echo '</ul>';

}
}

$post = $backup; // copy it back
wp_reset_query(); // to use the original query again

}

please adjust the bold sections

Good luck :)


Scott Hack comments:

No, still not doing anything.

2012-05-25

Agus Setiawan answers:

try this :

<?php
$post_type = 'yourposttype';
$tax = 'sponsorship';
$post_count = 21;
$counter = 0;
$tax_terms = get_terms($tax);
echo 'Related Advice';
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'orderby' => rand
);

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<ul class="sponsorship-post-list">';
while ($my_query->have_posts() && $counter < $post_count) : $my_query->the_post(); ?>
<li>
<h3 class="sponsorship-post"><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</li>
<?php
$counter += 1;
endwhile;
echo '</ul>';
}
wp_reset_query();
}
}
?>


Scott Hack comments:

Agus,

I saw this as well, but I am not filtering by $tax = sponsorship ... I think this is more restrictive than I need. I tried to use this earlier tonight and didn't get very far.