Support » Developing with WordPress » Related articles using a search query

  • matt-tyas


    Hello there. I hope the wordpress hive mind can help.

    I am creating a business directory (custom post type) that is filtered using the search and filter pro plugin. All is working well. However, I have a problem getting good matches on related articles from the default post type. These are articles about or mentioning the business in question as seen in this example:

    I have tried related post plugins but the results were not relevant enough. Ideally for the venue ‘bay horse tavern’ I want to return only articles specifically about the ‘bay horse tavern’. As you can see from the link the results I get do not to a user seem that related…

    I decided to use a query based on a search term to achieve this. The author can add a search term into a custom meta box on the directory that is then used in the template to build the query like so. It might be wrong – I am not the best with these things…

    $search_term = get_field('directory_related_posts_search_term');
    $args1 = array(
    'posts_per_page' => 3,
    's'              => $search_term,
    'post_type'      => 'post'
    $myposts1 = get_posts( $args1 );
    foreach ( $myposts1 as $post ) : setup_postdata( $post ); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" class="post-tile-link">
    <article <?php post_class('post-tile post-tile--grid'); ?>>
    <?php    						                      the_post_thumbnail('big-post-thumb');
    <h3><?php echo wp_trim_words ( the_title ( '', '', false ), 12 , '&hellip;'); ?></h3>
    <?php endforeach; wp_reset_postdata();?>

    This does not return particularly relevant results and interestingly does not return the same results as if I use the site search – which I find very odd.

    Does anyone know a) what the hell is going on? b) Should I search just on the post title perhaps, and how do I do this? c) Is there a better way to get to where I want to be?

    Thank you in advance!

    • This topic was modified 1 year ago by  matt-tyas.
Viewing 16 replies (of 16 total)
  • Moderator bcworkz


    You’re welcome. Whatever works is fair game. You deserve an award for most creative solution 🙂

Viewing 16 replies (of 16 total)
  • The topic ‘Related articles using a search query’ is closed to new replies.