1

Is it possible to fetch related post based on post title rather than based on tags and category? Like-
post 1 title : WordPress tips
post 2 title : WordPress tricks

In this example the related post should be generated on keyword WordPress picked from title instead of pulling it from used tags or category it's posted into.

Here is what i am using for now in functions.php

add_image_size( 'related', 100, 100, true );
add_action( 'genesis_after_post_content', 'child_related_posts' );
add_action( 'genesis_after_entry_content', 'child_related_posts' );

function child_related_posts() {

if ( is_single ( ) ) {

    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'             => 5,
            '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() . '" />';

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

                $postIDs[] = $post->ID;

                $count++;
            }
        }
    }

    if ( $count <= 4 ) {

        $catIDs = array( );

        foreach ( $cats as $cat ) {

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

        }

        $showposts = 5 - $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() . '" />';

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

    if ( $related ) {

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

    }

    wp_reset_query();

}
}

Since i am using genesis theme i got limit room to edit files usually restricted to editing functions.php and style.css in child theme.

Tried few other codes as well but this is closest i could get to things without plugin. The issue with this code is, it pull's related post based on tags rather then title of post.

3
  • What you've already tried? Without your try it seems like "do the job for me". Don't mind. :) But it indeed a very good idea. Dec 22, 2013 at 8:53
  • Previously i was using YARPP. Tried to work with few codes available on net but none proved satisfactory.
    – lucas
    Dec 22, 2013 at 15:14
  • Where is the related_posts_tags function defined?
    – s_ha_dum
    Dec 22, 2013 at 22:08

1 Answer 1

1

A simple WP_Query with an s argument will search the post title and the post content:

$args = array(
  'post_type' => 'post',
  's' => 'keyword'
);
$query = new WP_Query($args);
var_dump($query->request);

But you could further restrict that with one of several filters:

function restrict_search($search,$s) {
  remove_filter('posts_search','restrict_search');
  global $wpdb;
  return $wpdb->prepare(" AND {$wpdb->posts}.post_title LIKE '%%%s%%' ",$s->query['s']);
}
add_filter('posts_search','restrict_search',1,2);
$args = array(
  'post_type' => 'post',
  's' => 'keyword'
);
$query = new WP_Query($args);
var_dump($query->request);
5
  • Where are you expecting me to paste all this, in functions.php or single.php?
    – lucas
    Dec 22, 2013 at 19:23
  • @lucas : No idea. Your question provides no context that would allow me to guess the answer to that. I don't know if you are writing a plugin, building a theme... nothing. You asked: "Is it possible to fetch related post based on post title...?" Yes. This is one way to do that. I don't know how you need to use the code. You didn't explain that.
    – s_ha_dum
    Dec 22, 2013 at 19:59
  • I am really very sorry for it.
    – lucas
    Dec 22, 2013 at 20:26
  • Sorry? Why? I'm just letting you know that I can't answer the question unless you provide more detail.
    – s_ha_dum
    Dec 22, 2013 at 20:33
  • This is first time i am posting any question here, have no idea about how things work here and what all i need to share.
    – lucas
    Dec 22, 2013 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.