Invalid argument supplied for foreach()

You will get this error if foreach() is not an array. So your first step should be to debug this by inspecting the contents of $results to confirm that the results are what you expect. If it’s not then it might give you clues to why. This is debugging 101.

If you do that you’ll see that you’re not getting results. If you’re not getting results then the next step is to find out why. The way to do that is to check what you actually queried. You can do this by inspecting what the value of $prepare is.

If you do that then you’ll see that the SQL query does not match what you expect. In all likelihood the value for id_service= is missing, or incorrect.

Please note that absolutely none of these steps so far have required any WordPress development knowledge, special debugging tools or IDEs, or even any PHP knowledge beyond the very basics. Far more important than learning PHP or WordPress APIs is teaching yourself to follow these very very basic debugging steps.

So, if you followed that steps you’ll see that the id_service= part of the query has a missing or incorrect value. What would be the cause of this? You’re using $wpdb->prepare() to insert $id. But what is $id? On this line you’ve used explode() to create it:

$id = explode("-s", $id);

And what does explode() do? If you simply find the explode() article in the PHP manual you’ll see that it:

Returns an array of strings…

So, $id is an array of strings.

And what does $wpdb->prepare() do? It safely substitutes placeholders with variables in a string of SQL. As documented:

The following placeholders can be used in the query string: %d
(integer) %f (float) %s (string)

But in your code you’ve used %d, which is used to substitute an integer, but as noted above, $id is not an integer. It’s an array of strings. So that’s the cause if your problem: $id is not the right value for what you’re trying to do.

That’s as far as I can get without knowing all the bits and pieces of what you’re trying to do. But if you follow the debugging steps I’ve outlined above, you should be able to figure out where you’ve gone wrong.