Hello, everyone, welcome to part two of this lesson.
Let's pick up right where we left off.
So in part one, we added a new query to our search overlay results that will pull in professors based
on their relationship to a program.
The only problem is that we hard coded and faked a value of biology, which means at the moment it doesn't
even matter what phrase someone searches for.
So if I search for math, we still see the same two professors, and even if I just search for gibberish,
they still get pulled in.
So our goal for this lesson is to make this query actually dynamic and flexible.
So let's get started by jumping into our text editor and let's look inside our theme folders, ANC subfolder
and jump in to search R�big BHP.
And if we scroll down to the very bottom of this file right above this while loop down, here is our
new custom query.
And this is where a hardcoded a value of ninety seven, which is my biology program post.
So this query is saying, give us any professors where one of the related programs is biology.
But obviously we don't want to hard code a specific program here.
Instead, we need to find the ID number for any and all programs that match the current user search
phrase.
Now luckily we are already almost doing this on accident.
Let me show you what I mean.
So over in the Postman app, if I search for biology in the JSON that gets returned, there's an array
named programs.
Now, this result doesn't contain the ID number.
It only contains a title and permalink.
But we are the ones that control what gets output here.
So right now, let's just go adjust this program, Southbury, so that each item also has an ID property.
So back in the code, if we scroll up into our first while loop and find the if block for the program
post type if post type equals program, do this.
When we are building this array right after the permalink property, let's just say Kamma and we can
make up whatever property name we want.
I will just say ID and let's set it to equal get the ID, ok.
Now if we save that and refresh postman perfect.
Now any matching results for the program post type will include their ID number.
Now 97 is the number that I faked and hardcoded and that's because I'm performing a search for biology.
But up in the address bar, if I search for math, well, now within our program's array, we obviously
see a different ID number.
So now in that meta query that we are building, we have a real dynamic variable number that we can
use.
So back in the code, let's scroll down to find our new relationship query.
And instead of hard coding this to ninety seven, let's delete this entirely and let's say a pair of
single quotes with a double quote inside it and then add on the dynamic flexible ID number from our
programs sub array.
So let's look inside our results array for a summary named Programs and then let's look inside the first
item in that array and then let's look for a property named ID.
OK, now this will give us the value that we are looking for and then let's be sure to concatenate on
or add on a pair of single quotes with a single double quote within them.
Now if we save this and refresh postman, now when we search for math within the professor's array,
perfect, we actually receive the one professor that teaches math.
And this is exactly what we wanted.
So this means our relationship query is now paying attention to whatever phrase a visitor searches for.
So over on our front end, if we search for math, Dr. Rogerson.
But if we search for biology, perfect.
But we are not done just yet.
We still want to make our code a little bit more flexible.
Let me show you what I mean.
So at the moment, this value that we are using is looking within the first item or the first post within
the program's array.
Now, this set up will work as intended in certain instances, but what happens if there are multiple
programs in this array?
So, for example, over in Postman, if someone searches for math right now, we've just got the one
program named Math.
But what if in the future, our university has one program named Basic Math and another program named
Advanced Math?
Or if someone searched for.
Biology right now, we've just got the one program named biology, but what if in the future our university
has marine biology and human biology?
Right.
So what I'm getting at here is what if the program's array contains multiple programs?
Well, in that case, we wouldn't want to set this to only look within the first item or post in the
array.
So, for example, if someone searched for biology and we had both marine biology and human biology,
maybe the first item in the array zero would be human biology.
So this array as a whole, this filter would be looking for that.
And then what we would want to do is just add on another array and filter so you could copy this entire
array right after it, add a comma and then just paste it in again.
And then instead of looking for zero, the first item we would look for the second item or the one or
if we had a third biology program, we would add another filter.
Right.
So we'd say comma paste in the array again and change this to a two.
So this way we are pulling in any professors that have a relationship to maybe human biology, marine
biology and super duper advanced biology.
Now it is important to point out that by default, when you have multiple arrays or filters like this,
WordPress is default behavior is to try and make sure that all of these conditions are met.
And in our case, we don't want to make sure that this and this and this is Matt.
We're looking for this or this or this.
So in order to tell WordPress that that's how we want to handle things right within our main Outr array
here.
So maybe right before the first inor array, we can just say relations equals or sure to include a comma
here.
Now, moving on, there's just one problem with our code, and it's a pretty big problem.
This code only makes sense if there are exactly three programs.
What if there were four or five or what if there was only one?
Well, then this code wouldn't make any sense.
And within the code here, we have no way of predicting how many programs there's going to be, which
means we can't code in this ultra repetitive spelled out way.
Instead, we need to create these arrays in a programmatic way that will work, whether there is one
or 10 programs.
So the way that I would do this is instead of spelling out the array manually right here, instead of
that maybe right above this query, let's add a new line and let's create a variable that will be an
array and then we can just pass that variable right here.
So we could call this variable whatever we want.
I'm going to call it programs meta query and set it to equal an array.
Now, this array down here is basically our goal.
We're just trying to create this programmatically.
So we are still going to want the first thing in the array to be relation equals or so up within our
new array.
Let's say relation equals four.
And for now, that's the only thing that I'm going to manually add on to the array when we are first
creating it.
Now our next step is to loop through the results program's array.
And for each item in that array, we want to generate a filter array and added on to this array.
I realized that sentence was probably impossible to follow because I said the word array 20 times,
but let me show you what I mean.
So right below this line, let's create a for each loop.
So for each parentheses curly brackets within the parentheses, four for each.
The first thing you give it is an array that you want to work with.
In our case, that's the results program's SOUTHBURY and then still within the parentheses, right after
that array that you select, you can say as and then you can make up a variable name.
So I'm just going to say as item.
All right.
Now, within the curly brackets, as the foreach loop loops through this array, we can access the current
item with this variable name.
OK, so what do we want to do?
We want to add on to this new array that we're building.
So within our for each loop, let's say, array push.
This is the function that lets you add on to an existing array.
We give it to arguments.
The first is the array that you want to add on to, which in our case is our brand new programs, meta
query programs, meta query.
And then the second argument is what you want to add on to the array, which.
In our case, is just one of these three filter arrays, so it doesn't matter which one, just grab
any of them.
Copy that.
Back up in our array, push for the second argument pasted in.
And now the important part is the value that we want to search for.
So let's leave the quotes and the period to add on and leave them at the end here as well.
But let's delete this entire inner chunk and the value that we want to search for is item, which is
the current post that's been looped to at this moment.
And then we want to look inside that for the property of ID.
All right.
Now, at this point, we have successfully rebuilt this array and we've done so in a programmatic way
that will work whether there's one or twenty programs.
So down within our query, we can delete this entire Outr array.
And instead, let's just say medical theory equals the name of that brand new array we just built,
which is programs, medical query, and if we go ahead and save this and test out postman again.
A search of biology still gives us what we would expect, and if I search for math, perfect, we just
received Dr. Rogosin.
So our code is working as expected, and now we can rest easily knowing that it will work even if we
have more and more programmed names in the future.
However, if I perform a search for just gibberish that shouldn't have any results, we see that that
returns all professors.
So clearly there's an issue with their code that we need to fix.
And the issue has to do with our new query that we created using the WP query class.
Basically, the issue is if the current search phrase doesn't return, any program results.
That means that we are basically telling the meta query to be empty because this array is created by
looping through any and all program results.
And if meta query is empty, we're basically just saying with this query, give us all professor posts,
which is obviously not what we want.
So to fix this, here's what I'm going to do right above this line where we create our meta query array
right above that.
Let's write an if statement and for the condition, let's say only if results programs exists.
So only if it's not empty do we want to do this and what do we want to do?
Well, let's just cut and paste all of this code.
So beginning with the array that we are creating and selecting all the way down right before the return
line, I'm going to cut that and then just paste it to live within the if statement.
Cool.
So now all of the code that we wrote in this lesson will only be executed if there are indeed programs
to try and find a relationship with.
So if we save that and back in postmen, if I try this gibberish search again, perfect zero results.
And if I search for math again, Dr. Furguson, which means our code is still working as expected.
Now, before we bring this lesson to a close, I do want to point out a flaw with our search, logic
and code.
And then I will also show you a quick fix to the problem.
It's actually easier to show you visually the problem instead of trying to explain it just with words.
But check this out over in my WordPress admin dashboard.
If I go into programs and if I go into biology, let's imagine I want to update the main body text.
So maybe I would say the biology program works closely with the math program.
The key here is that I mentioned the name of another program within this main content field.
Right.
We've got the word math in here now.
So if I save that now, if anyone on the front end searches for math, well, unfortunately, it's going
to pull in biology and anything that has a relationship to biology.
And this is not what we want.
If someone searches for math, there's no reason we should show them the biology program or biology
professors.
So we need to adjust things so that our search logic only searches the title field when it comes to
programs.
It's OK to search the main content field if it's a blog post or a page.
But when it comes to programs, we don't need to search the main content field because it's going to
pick up on any keywords in here and probably throw a bunch of false positives.
Now, in order to customize our search logic like this, we have two main options.
Option number one would be to write a bit of our own handcrafted sequel.
Right.
We could sort of hold WordPress as hand as it talks to our MySQL database.
But I don't ever like to customize the default WordPress SQL statements unless I absolutely 100 percent
need to.
So our second option and the option that I'm going to use is we can create an advanced custom field
that looks exactly like this main body content field and use that for programs instead of this default
field.
And this will work because WordPress will not search within that advanced custom field.
So let me show you what I mean and how I would accomplish this over in the sidebar.
Let's click on Custom Fields down here towards the bottom.
And let's go ahead and create a brand new field group, so ad new, we could name it whatever we want,
but I'm going to call it main body content.
Then let's go ahead and create a new field.
So click add field.
I will title it main body content.
As soon as you click out of that field, they will automatically generate this name for you all lowercase
with underscores instead of spaces.
And then the important part is for field type.
Instead of just text, let's choose WYSIWYG editor.
That abbreviation stands for.
What you see is what you get.
The rest of these default values are OK and we can scroll down until we see this location box and let's
say only show this field group if post type is equal to and then let's choose program.
OK, and before we save this, let's be sure to go down to the options box and for the position option
instead of normal, let's choose high after title that will make sure that this field is positioned
at the very top of the author's screen right below the title field.
So that way it's up at the top.
Very easy to find, just like the default content field.
So now I'm going to scroll up and choose, publish.
And now if I go back into programs and choose to edit the biology program, here is our brand new custom
field.
And this is the default WordPress content field.
So I'm just going to cut and paste the content out of that field and into our new custom field.
And then if I save that.
The WordPress search logic will no longer be able to find the word math within this Buyology post because
WordPress is only going to look within this default field.
So, for example, back on the front end, if I search for math now, perfect, we're back to normal
and we don't have any false positives.
However, we do need to edit our template for individual program pages.
We need to tell the template to use this new field.
Right.
So, for example, if I go to programs and then biology, we don't see the main body content here because
it's still using that emptied out default field.
So to fix that, all we need to do in our theme folder is jump into single program dot p and write where
you see the content.
This is what would get the default field.
Let's replace that with the field.
And we named it main body content.
Give that a save refresh.
Perfect.
Now, the one other thing we need to take care of is back on the author screen.
This is super confusing.
We don't need to content fields.
So if this is the one we're using, we should just entirely hide the default one.
Now, luckily, it's very easy to do that.
We just need to edit the code that we wrote when we first registered the program post type.
So in our text editor, not within our theme folder, but within our You Must Use Plugins folder.
If we jump into that file, if we scroll down a bit and find the register post type for the program,
type within the supports equals array, we can just remove Ed.
Ed is the main default content field.
So if I just delete that and save this, then back in our admin perfect.
That default content field is completely gone and that's going to bring this lesson to a close.
In our next lesson, we will finally finish our search overlay.
In the next video, we are going to work on displaying campuses and events based on a relationship they
might have to a program.
Let's keep things rolling and I will see you then.