Fairly new to wordpress, but Im trying to do what should be fairly straight forward. I want to get a list of all "departments" (a custom taxonomy). and then all the posts in those departments. A post can be in more than one department.
All the examples I have found seems to get the departments then loop through them and get the posts, but this seems like an inefficient way to query the DB.
So my end result would hopefully look like this:
[
"Department 1" => [
0 => post 1,
1 => post 2,
2 => post 3
],
"Department 2" => [
0 => post 1,
1 => post 2,
2 => post 4
],
]
Note, some posts appearing under multiple departments.
Can anyone point me in the right direction?
Thanks in advance
2
Answers
Please try with this code. May is useful for you. Thanks
You use the
posts_clauses
filter hook and modify theSQL
query based on your requirements. check below code.and remove filter so it’s only apply to your query not all query.