Search code examples
thumbnailswordpressrelated-content

How to display related post by categories with next post of current post (don't latest post or random post)


How to display related post by categories with next post of current post (don't latest post or random post). I'm use code related post for twentytwelve theme. but now, author in wentytwelve_entry_meta() is repeat.

pls help me :

<div id="related_posts">
    <?php
        $categories = get_the_category($post->ID);
        if ($categories) {
            $category_ids = array();
            foreach((get_the_category()) as $category) {
                $id = $category->cat_ID;
            }
            global $wpdb;
            $query = "
                SELECT * FROM $wpdb->posts
                LEFT JOIN $wpdb->term_relationships ON
                ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
                LEFT JOIN $wpdb->term_taxonomy ON
                ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
                WHERE $wpdb->posts.post_status = 'publish'
                AND $wpdb->term_taxonomy.taxonomy = 'category'
                AND $wpdb->term_taxonomy.term_id = $category_ids[0]
                AND $wpdb->posts.id < $post->ID
                ORDER BY id DESC limit 3
           ";
           $my_query = $wpdb->get_results($query);
           if( $my_query) {
               echo '<h3>Related Posts</h3><ul>';
               foreach($my_query as $key => $post) {
               ?>
                   <li>
                        <div class="entry-header">
                            <div class="header-l">
                                 <h1 class="entry-title">
                                      <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                                </h1>
                                <p class="datetime">
                                    <?php twentytwelve_entry_meta(); ?>
                                </p>
                            </div>
                            <?php the_post_thumbnail(); ?>
                      </div>

                      <div class="entry-summary">
                          <?php
                              $str_content = wp_trim_words($post->post_content);
                              $str_content = str_replace('[', '<', $str_content);
                              $str_content = str_replace(']', '>', $str_content);
                              echo $str_content;
                          ?>
                     </div>
                </li>
           <?php
           }
           echo '</ul>';
       }
   }?>


Solution

  • WordPress customary is to use WP_Query class to fetch posts from DB, if you can modify your code to use WP_Query it would be easier.

    WP_Query Reference

    As you are using Custom Query to load your posts from DB, template tags like the_permalink(), the_title_attribute(), the_title() etc will not work properly that is the reason while theme function twentytwelve_entry_meta() fails.

    As per codex reference Displaying Posts Using a Custom Select Query

    You should try something like this:

    global $wpdb;
    $query = "
        SELECT * FROM $wpdb->posts
        LEFT JOIN $wpdb->term_relationships ON
        ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
        LEFT JOIN $wpdb->term_taxonomy ON
        ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
        WHERE $wpdb->posts.post_status = 'publish'
        AND $wpdb->term_taxonomy.taxonomy = 'category'
        AND $wpdb->term_taxonomy.term_id = $category_ids[0]
        AND $wpdb->posts.id < $post->ID
        ORDER BY id DESC limit 3";
    
    $my_query = $wpdb->get_results($query);
    
    if($my_query) {
        global $post;
        echo '<h3>Related Posts</h3><ul>';
        foreach($my_query as $key => $post) {
            //use setup postdata, it works only for variable named $post.
    
            setup_postdata($post); 
            //you can safely use template tags now.
    
            ?>
            <li>
                <div class="entry-header">
                    <div class="header-l">
                        <h1 class="entry-title">
                            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                        </h1>
                        <p class="datetime">
                            <?php twentytwelve_entry_meta(); ?>
                        </p>
                    </div>
                    <?php the_post_thumbnail(); ?>
                </div>
    
    
                <div class="entry-summary">
                  <?php
                      $str_content = wp_trim_words($post->post_content);
                      $str_content = str_replace('[', '<', $str_content);
                      $str_content = str_replace(']', '>', $str_content);
                      echo $str_content;
                  ?>
                </div>
            </li>
            <?php
        }
    }
    

    Other interesting post:

    What does setup_postdata ($post ) do?