Hello, everyone.
In this lesson, we will learn how to implement custom search logic that is capable of searching by
relationship.
Let me show you what our goal is.
So at the moment, if someone searches for biology, that's only going to pull in results that contain
the word biology, either in the title or the main body content field, which means it's going to ignore
professors that have their subject set to biology.
And that's obviously not what we want.
We would want our biology professors to appear right here in the results.
Same thing with events.
Any biology related events should display here.
Way back earlier in the course, we learned how to set up relationships between one post and another.
Well, now, in this lesson, our goal is to write custom code that makes our search aware of those
relationships.
So right now, let's work on making that happen.
Now, I want to begin by saying that we do not need to learn any new code or functions in this lesson
in order to achieve our goal.
We've already learned all of the technical tools and code.
And now the challenge at this point has very little to do with coding, and the challenge has more to
do with thinking critically and mapping out a solution.
So before we write any code, let's first create a plan in plain English.
That way we're not stressing about semicolons and parentheses and code syntax, and we can use all of
our brainpower on mapping out steps to a solution.
Now, I know this is easier said than done.
It's still really challenging to decide what you need to do.
But a little trick that a mentor of mine taught me is to work backwards.
What I mean by work backwards is don't try to map out your code solution from beginning to end.
Instead, focus first on your end goal and then sort of reverse engineer or work backwards from there.
Now, I know that everything I just said was super generic.
So now let's apply this to the actual task at hand.
So if I'm going to work backwards, that means that at first I'm not even going to worry about the phrase
or the search term that the user typed in.
Instead, I'm going to focus all of my energy on the thing that we want to pull in.
Right.
Which is related.
Prof's So right now, why don't we go look at an example of a professor post over in the WordPress admin
back and so over in my dashboard.
I'm going to click on Prof's in the sidebar.
And let me click on a professor that I know teaches biology in my case, that's Dr. Bachs a lot.
And if I scroll down to the related programs field, we see that this post does indeed have a relationship
with the biology program.
Now, even though you and I see the word biology here, WordPress does not store the word biology in
the database for this field for professor barks a lot.
Instead, it only stores the numeric I.D. for the biology program post, which on my website is number
97.
But it's almost definitely a different number on your site.
Now, before I move on and explain why this matters so much, I first want to show you how you can find
the ID number of a related program.
So, for example, if I wanted to find the ID of the biology program, I could just click programs in
my sidebar and then click on Biology.
And then up in my address bar I can see post equals and then a number.
So throughout the next part of this lesson, I'm going to be using the number ninety seven.
But for you it's almost definitely a different number.
So take note of whatever number you see here on your WordPress website.
The key point is that I just want you to have an ID number of a program post that, you know, professors
have been assigned to.
OK, so with your ID number in mind, let me go back to Professor Bachs lot.
And the reason I bring up all of these issues regarding the ID number is because we can't search the
database for professors where the related program equals the word biology.
Instead, we need to search the database for instances where this field equals 97 or whatever your number
is.
Now, luckily, we already know how to do that.
Right.
For example, if we go to our website and up in the header, if we click on programs.
And then if I click on Buyology.
We've already learned how to write that exact query.
We've already learned how to query for professors that have a relationship to a specific ID number.
So our first task in terms of working backwards is let's just implement a query like this and we can
use a hard coded value of ID number 97 or whatever your number is.
And let's just work on integrating that query into our custom API URL.
Jason results.
That was a lot of buzzwords.
So what I really mean is if you jump over to the Postman app and if you target our new custom, APU
�rale and say questionmark term equals biology.
At the moment we don't see any prof's because obviously we haven't written that custom logic yet.
And what I'm proposing for our first step in terms of working backwards is let's just hard code a query
that pulls in any prof's based on an ID number of 97 or whatever your number is.
But the idea is let's just hard and fake it to always look for biology at first.
And then once we have that up and running, then we can hollow it out and make it dynamic and actually
look for the correct ID number based on whatever someone is searching for.
OK, but for now, let's not get ahead of ourselves for now.
Let's just fake our query to always be for biology.
So right now, let's finally go write some code.
We know that if we want to modify the JSON that this Eurail returns, we just need to jump into our
text editor and within our theme folder, look within the ANC folder and jump in to search dash root
around line number 13.
You can see we've already created one custom query by using the WP query class.
And all we are doing here is searching through all of our post types based on a keyword.
So this is our basic first layer of the search.
But now we want to go deeper.
We want to search based on relationships.
So I'm actually going to create another separate query so we can scroll all the way down to the bottom
of this file.
And right before we return, the results right above that, let's just add a new line and notice my
cursor is below this closing curly bracket for the while loop that's up above.
So right here, let's just create a brand new query.
So I'm going to create a variable.
I could name it whatever I want.
The name does not matter.
I will call it program relationship query and set it to equal a new instance of the WP query class.
And then within those parentheses, let's pass it in array.
Within that array, let's dropdown.
And first, let's say that post type should be professor.
That's what we're looking for.
A bit later on in the lesson we can update this to also look for events.
But for now, let's just say Professor comma and then let's also provide a meta query, because that's
how we can search based on the value of a custom field.
So we want to set that to be an array.
And then you might remember from earlier in the course within this array, we actually provide another
array.
The reason we do this is because WordPress lets you string together multiple filters, meaning you could
have multiple inner arrays, each one being a filter, and they are nested within the outer array.
All right.
Now let's focus on building this inner array.
We want to code a filter that looks within the related programs, custom field.
So just to keep my sanity, I'm going to drop down onto a new line right before the A of this inner
array.
And then I will drop down again in between the two closing parentheses of the inner and outer array.
Cool.
So now let's just build out this filter.
So within the inner array, when a dropdown again and I'm going to give it three properties or three
arguments, the first one is key.
So just set that to be a temporary X.
Second one is Compar.
And the third and final is value.
OK, let's go through these one by one.
The key is just the name of the advanced custom field that we want to look within.
So back in the WordPress admin, it would just be whatever the short name, variable name for this custom
field of related programs is.
So instead of the placeholder X, I will say related underscore programs for compare, there are different
ways of comparing values.
In this case, we are not looking for an exact match, nor are we comparing actual numbers.
We are actually searching for numbers disguised as strings nested inside an array.
So long story short, the COMPAR method that we are going to use is like and then finally for value.
Remember, we're just going to hard code this to always be looking for biology.
And in my case that's an ID number of 97, but it might be a different number for you.
So instead of X, let's say single quotes, and then within the single quotes, let's say double quotes
and then our number, if you're wondering what's going on with the double quotes and single quotes here,
you might just want to jump back to earlier in the course when we created our first made a query.
Back then, we went over the details and reasoning for this in-depth.
OK, now, at this point, we've got our new custom query next.
We just want to loop through it and push each of the results onto the relevant empty array from way
up here.
Remember, we've got these different arrays for each post type, so just like we did with our main query,
we looped through the results and put them in the appropriate location.
Now, we just want to do that for our new query.
So back down at the bottom of this file, right below our new custom query.
Let's just set up a while, loop parentheses, curly brackets for the while condition.
We just type in the name of this query variable.
So program relation, ship query, look inside it for a method named have posts.
OK then within the wire loop, you always want to begin by calling the method named the Post.
So let me type out my annoyingly long query name again.
Program relationship query.
Look inside it and call the method the post OK.
And now we can do whatever we want, but there's no sense in reinventing the wheel because if we scroll
up just a bit.
We've already got this if block that says if the post type is a professor, then create an array and
push it onto the results.
Professor Southbury, now this is exactly what we want to do down in our new wire loop.
So I'm just going to copy this if block for the professor post type.
Let's copy that.
And then down towards the very bottom of the file in our new wire loop right after this.
The post line.
Let's create a little bit of space and then just paste in the clipboard.
And now if we save this and refresh postman.
Perfect within the professor's Arae, we now see the two professors that teach biology barks a lot and
meows a lot.
Cool.
So we've got this working now.
We just need to continue working backwards because remember that we faked this and we hardcoded it to
always look for biology.
Now, before we move on to the next step and learn how to search for the appropriate ID number based
on the search phrase that the user is actually looking for.
Before we address that, I first want to cover the possible issue of duplicate results, because now
that we are running two queries, it's possible for a post to show up in both of the queries.
Let me give you an example.
So what if the doctor barks a lot post contained the word biology in its main body content field.
Well, then it would show up once in our generic keyword query and then it would show up again in our
relationship query.
Let's test that out really quick.
So over in the admin dashboard, here I am and the doctor barks a lot post.
And if I updated the content field to say I love biology and go ahead and save.
Now, if I go back to Postman and refresh within the professor's array, we see two instances of doctor
barks a lot.
And the reason for this we jump back to our code is because barks a lot will now show up in this main
query that searches the title and body field for a keyword and barks a lot, is also going to show up
in our custom query.
So all we need to do to fix this is right.
Before we return the results to the API, we just want to run a function that will remove duplicates
from our array.
So to do that, let's target our results, Prof's Southbury and let's set this to equal the results
of a P function named Array Unique.
And we want to give this function to things so we could say A and B, the first thing we give it is
an array that we want to work with.
So let's just give it our Prof's array.
So results square brackets, professors.
And for the second argument, we just want to tell this function to play nicely with associative arrays,
meaning please look within each sub item of an array when you're trying to determine if they are a duplicate
or not.
So instead of B here, let's say sort of underscore regular.
OK, now if we go ahead and save this, we should see that array unique removes any and all duplicates.
So over in Posman, if we refresh, cool.
Now we've only got one.
Dr. Barks a lot again.
However, we do see that that array unique function provides a numerical key for each item.
Right.
We didn't have this zero and two just a moment ago.
And in order to stay super neat and organized, I don't want this.
So to get rid of those, what we can do is wrap this array unique within something called array values.
So check this out right before array unique, say, array, underscore values.
And then I'm just going to place an opening parentheses here and then this needs a matching closing
parentheses right down here.
Cool.
So let's give that a try.
Save it.
Refresh postman.
Perfect.
And that's going to bring part one of this lesson to a close.
In our next video.
We will adjust our solution so that it's not hard coded to always look for biology and instead actually
pay attention to whatever phrase the user has searched for.
Let's keep things rolling and I will see you then.