Sorry if this is a duplicate, but I didn’t find it anywhere.
I need to get all the products belonging to a certain vendor via a raw SQL query.
I know both the vendor slug or id, but I couldn’t figure out exactly the relationship between the products and a vendor.
Can anyone share a complete SQL to do that or at least what is that relationship between tables.
Thanks for any advice!
2
Answers
I tried it and it works, thanks! :) One problem is that obviously you don't have access to the WP functions from an external script/language, but I found a solution that I share here too.
You can include the WP function with this line
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
So I created a PHP file where I used the above code and I generated a json structure with all the information I needed. Then I can access that from any external script/language. It's not raw SQL, but it does that job.
Hope this solution helps someone that needs this.
Here’s the code that I used on one of my sites:
Note: Replace the $vendor_id with the ID of the vendor you have.
Note: It is not recommended to do RAW SQL in WordPress. Try to stick to the standard methods available to get desired output.