1
votes

I have created a related post function and adding it into wordpress functions.php.

function related_posts($args = array()) {
    global $post;

    // default args
    $args = wp_parse_args($args, array(
        'post_id' => !empty($post) ? $post->ID : '',
        'taxonomy' => 'category',
        'limit' => 4,
        'post_type' => !empty($post) ? $post->post_type : 'post',
        'orderby' => 'date',
        'order' => 'DESC'
    ));

    // check taxonomy
    if (!taxonomy_exists($args['taxonomy'])) {
        return;
    }

    // post taxonomies
    $taxonomies = wp_get_post_terms($args['post_id'], $args['taxonomy'], array('fields' => 'ids'));
    if (empty($taxonomies)) {
        return;
    }

    // query
    //  $related_posts = get_posts(array(
    $related_posts = new WP_Query(array(
        'post__not_in' => (array) $args['post_id'],
        'post_type' => $args['post_type'],
                'tax_query' => array(
                array(
                   'taxonomy' => $args['taxonomy'],
                   'field' => 'term_id',
                   'terms' => $taxonomies
                ),
            ),
            'posts_per_page' => $args['limit'],
            'orderby' => $args['orderby'],
            'order' => $args['order']
        ));

        if  $related_posts ) {
            echo 'ok';
        } else {
            echo 'not ok';
        }
    ?>
    <?php if (!empty($related_posts)) { ?>
        <h3 class="widget-title"><?php _e('<h5 class="title is-6">You Might Also Like</h5>', 'http://localhost/wordpress_john/wordpress1/'); ?></h3>

        <div class="columns  ">
        <?php
           include( locate_template('related-posts-template.php', false, false) );
        ?>
        </div>
        <?php
        }
        ?>
        <?php
             wp_reset_postdata();
        }

        // related posts
        add_action( 'comment_form_before', 'related_posts', 10, 0 ) ;

I have created a custom post ( post_type => 'custom' ) and its template etc which is working fine. But this code is not showing related posts when viewer is viewing custom post that is being served from single-custom.php Initially this code was with get_posts and I converted it into WP_QUERY since code was returning empty from get_posts, why? Single.php's posts were showing related posts as they should but not single-custom.php. Then I converted into WP_QUERY since get_posts is a bit restricted then WP_QUERY and still, custom post (single-custom.php ) is showing no related posts but variable $related_posts is being populated here. Help!

1

1 Answers

1
votes

Try this working code:

 <?php $customTaxonomyTerms = wp_get_object_terms( $post->ID, 'category', array('fields' => 'ids') );

        $args = array(
            'post_type' => 'events',
            'post_status' => 'publish',
            'posts_per_page' => 5,
            'orderby' => 'rand',
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => $customTaxonomyTerms
                )
            ),
            'post__not_in' => array ($post->ID),
        );

        //the query
        $relatedPosts = new WP_Query( $args );

        //loop through query
        if($relatedPosts->have_posts()){
            echo '<ul>';
            while($relatedPosts->have_posts()){ 
               $relatedPosts->the_post(); ?>
               <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
               <?php } echo '</ul>';
             }else{
            //no posts found
            }
          wp_reset_postdata();
        ?>