Search query – exact post title match

I have a bunch of posts titled something like this…

English Language 2015
English Language 2016
English Language 2017
English Language 2018

And on each one of these posts I have a dropdown select menu displaying posts with the same title, but different year. Here is my code for this. I am using Relavanssi plugin by the way:

<?php global $post;

$the_title = preg_replace(‘/[0-9]+/’, ‘ ‘, $post->post_title);

$args = array(
‘post_type’ => ‘product’,
‘s’ => $the_title,
‘sentence’ => true,
‘exact’ => true,
‘orderby’ => ‘title’,
‘order’ => ‘ASC’,
‘showposts’ => -1
);

$query = new WP_query($args);

relevanssi_do_query($query);

if ($query->post_count > 1) { ?>

<h4>Years</h4>

<select onchange=”location = this.value;”>

<option disabled selected value>Select Year</option>

<?php

foreach ($query->posts as $r_post) {

if ($r_post->ID == $post->ID ) continue;
$link = get_permalink($r_post->ID);
$title = get_the_title($r_post->ID);

echo “<option value=’$link’>$title</option>”;

}

?>

</select>

<?php } ?>

As you can see, I’m looking for the current post title and removing the years.

All good, HOWEVER, if I have more posts called something like…

English Language and Literature 2015
English Language and Literature 2016
English Language and Literature 2017
English Language and Literature 2018

…these posts will also display in the dropdown menu in the above ‘English Language’ posts, as it’s picking up the phrase ‘English Language’, but I want an exact search query so it only displays ‘English Language’ posts on the dropdown of these posts.

I tried wrapping the $the_title var in quotes in attempt to do a phrase search but this did nothing.

Read more here:: Search query – exact post title match

Leave a Reply

Your email address will not be published. Required fields are marked *