Passing Data to Blade Template Partial in Custom WP_Query

“Best practice” here is, to some extent, whatever works for you, but the approach I tend toward is to set up and run all necessary logic (apart from simple conditionals) at the controller level, and then pass only what the Blade needs to the Blade. This means I never really use WordPress functions in my Blades—I just echo variables passed from my controller.

Usually this comes down to doing some more advanced data construction/manipulation at the controller level, instead of just passing queries on through. You can see a more in-depth example of this here: The right way to run multiple WordPress loops

In your case, you could accomplish that with an array_map on your initial query that processes the data for partials.list-item-person (In fact, you could event construct the partial name in the controller, and avoid the concatenation in your template, which would allow you to just do: @include($post['partial_name'], $post['data'])).

2 Likes