Related post without plugin for custom post types

I need some help modifying this script. I am trying to also show related posts by custom taxonomies and custom post types.

//* Define custom image size for featured image in Related Posts
add_image_size( 'related', 300, 0, true );


add_action( 'genesis_after_entry_content', 'child_related_posts' );
/**
 * Outputs related posts with thumbnail
 *
 * @author Nick the Geek
 * @url http://designsbynickthegeek.com/tutorials/related-posts-genesis
 * @global object $post
 */
function child_related_posts() {

    if ( is_singular ( ) ) {

        global $post;

        $count = 0;
        $postIDs = array( $post->ID );
        $related = '';
        $tags = wp_get_post_tags( $post->ID );
        $cats = wp_get_post_categories( $post->ID );

        if ( $tags ) {

            foreach ( $tags as $tag ) {

                $tagID[] = $tag->term_id;

            }

            $args = array(
                'tag__in'               => $tagID,
                'post__not_in'          => $postIDs,
                'showposts'             => 8,
                'ignore_sticky_posts'   => 1,
                'tax_query'             => array(
                    array(
                                        'taxonomy'  => 'post_format',
                                        'field'     => 'slug',
                                        'terms'     => array(
                                            'post-format-link',
                                            'post-format-status',
                                            'post-format-aside',
                                            'post-format-quote'
                                            ),
                                        'operator'  => 'NOT IN'
                    )
                )
            );

            $tag_query = new WP_Query( $args );

            if ( $tag_query->have_posts() ) {

                while ( $tag_query->have_posts() ) {

                    $tag_query->the_post();

                    // $img = genesis_get_image() ? genesis_get_image( array( 'size' => 'related' ) ) : '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/related.png" alt="' . get_the_title() . '" />';
                    $img = genesis_get_image( array( 'size' => 'related' ) );

                    $related .= '<div class="related-post"><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . $img . get_the_title() . '</a></div>';

                    $postIDs[] = $post->ID;

                    $count++;
                }
            }
        }

        if ( $count <= 7 ) {

            $catIDs = array( );

            foreach ( $cats as $cat ) {

                if ( 6 == $cat )
                    continue;
                $catIDs[] = $cat;

            }

            $showposts = 8 - $count;

            $args = array(
                'category__in'          => $catIDs,
                'post__not_in'          => $postIDs,
                'showposts'             => $showposts,
                'ignore_sticky_posts'   => 1,
                'orderby'               => 'rand',
                'tax_query'             => array(
                                    array(
                                        'taxonomy'  => 'post_format',
                                        'field'     => 'slug',
                                        'terms'     => array(
                                            'post-format-link',
                                            'post-format-status',
                                            'post-format-aside',
                                            'post-format-quote' ),
                                        'operator' => 'NOT IN'
                                    )
                )
            );

            $cat_query = new WP_Query( $args );

            if ( $cat_query->have_posts() ) {

                while ( $cat_query->have_posts() ) {

                    $cat_query->the_post();

                    // $img = genesis_get_image() ? genesis_get_image( array( 'size' => 'related' ) ) : '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/related.png" alt="' . get_the_title() . '" />';
                    $img = genesis_get_image( array( 'size' => 'related' ) );

                    $related .= '<div class="related-post"><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . $img . get_the_title() . '</a></div>';
                }
            }
        }

        if ( $related ) {

            printf( '<div class="related"><h3 class="related-title">Related Posts</h3><div class="related-posts-list" data-columns>%s</div></div>', $related );

        }

        wp_reset_query();

    }
}

//* Enqueue and initialize jQuery Masonry script
add_action( 'wp_enqueue_scripts', 'sk_masonry_script' );
function sk_masonry_script() {

    if ( is_singular( 'post' ) ) {

        wp_enqueue_script( 'masonry-init', get_stylesheet_directory_uri().'/js/masonry-init.js', array( 'jquery-masonry' ), '1.0', true );

    }

}

Here are the post types that I have:

1: post (tags, categories)

2: custom_post_type: (4 custom taxonomies)

3: custom_post_type: (2 custom taxonomies)

4: custom_post_type: (1 custom taxonomies)

I was able to make some adjustments by just looking at the code and was able to get custom post types related posts to show up. However, i am sure that there is someone better than I am out there who can make this work.

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Related post without plugin for custom post types

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.