WordPress.org

Forums

Fix Incorrect related posts code snippet (9 posts)

  1. Arafin Shaon
    Blocked
    Posted 2 years ago #

    Can anybody fix this incorrect related posts codes snippet?

    function related_posts() {
    if (is_single()) {
        global $post;
        $current_post = $post->ID;
        $categories = get_the_category();
    foreach ($categories as $category) :
        ?>
    <div class="my-related-posts"><h4>Related Posts</h4><ul>
        <?php
        $posts = get_posts('numberposts=5&category='. $category->term_id . '&exclude=' . $current_post);
    foreach($posts as $post) :
        ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endforeach; ?><?php endforeach; ?>
    </ul>
    </div>
        <?php
            }
        wp_reset_query();
    }
    add_action( 'genesis_after_post_content', 'related_posts' );

    This snippet works fine to display related posts specifically for parent category. Problem is happening after publishing a post under child category (like under “Social Media -> Facebook”. # Related posts being displayed twice one for parent and other for child category. Which i don't want.

    preview: http://i.stack.imgur.com/F7tuS.png

    Related posts should specifically show based on that particular category, that under the post is published no matter whether it's parent or child category. Can anybody help me regarding this ? Any help would be appreciated :)

  2. Tara
    Volunteer Moderator
    Posted 2 years ago #

  3. Arafin Shaon
    Blocked
    Posted 2 years ago #

    already asked there but still no response from anybody :(

  4. Tara
    Volunteer Moderator
    Posted 2 years ago #

    Try this snippet. It works for me.

    <?php
    // First, backup the default $postdata
    $backup = $post;
    // Now, override the default
    $tags = wp_get_post_tags($post->ID);
    // Now, open the if ( $tags ) statement
    if ($tags) {
        $tag_ids = array();
        foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
        // Now, setup your custom query args
        $args=array(
            'tag__in' => $tag_ids,
            'post__not_in' => array($post->ID),
            'showposts'=>10, // Number of related posts that will be shown.
            'caller_get_posts'=>1
        );
        // Now, perform your custom query
        $my_query = new wp_query($args);
        // Next, open your custom query IF statement
        if( $my_query->have_posts() ) {
            // We have posts, so let's output the opening-containing markup
            ?>
    		<div class="relatedposts">
            <h3>Related:</h3> 
    
                    <?php
                    // Now, open your custom query WHILE statement
                    while ($my_query->have_posts()) {
                        $my_query->the_post();
                        ?>
        <ul><li>      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li></ul>
                         <?php
                        // Now, close the while $my_query->have_posts() statement
                    }
                    // Now, output your closing-containing HTML content
                    ?>
    </div>
            <?php
        // Now, close the if $my_query->have_posts() statement
        // and open the ELSE statement, for your no-posts content
        } else {
            ?>
            <?php // If there are no related posts ?>
            <p class="noposts">No related posts found</p>
            <?php
        // Now, close the ELSE statement
        }
    // Now, close the if ( $tags ) statement
    }
    // Now, reset the default query
    $post = $backup;
    wp_reset_query();
    ?>
  5. Arafin Shaon
    Blocked
    Posted 2 years ago #

    @t-p tnx for this. But its tag based but i need category based related posts...

  6. Tara
    Volunteer Moderator
    Posted 2 years ago #

    may be some else can chime in.

  7. Arafin Shaon
    Blocked
    Posted 2 years ago #

    i hope so :)

  8. Tara
    Volunteer Moderator
    Posted 2 years ago #

    here is an idea: combine your code and mine code and make it both category+tag based!!!

  9. bcworkz
    Member
    Posted 2 years ago #

    Using the 'posts_distinct' filter should solve the problem. Plugin API/Filter Reference/posts distinct. I would have thought this would be done already for you for category queries, there's probably some reason that I'm not seeing.

    You may want to use remove_filter() after your query to restore default conditions.

Topic Closed

This topic has been closed to new replies.

About this Topic