使用 SQL 查询还获取 WooCommerce 产品类别缩略图 ID
Get also WooCommerce product category thumbnail Id using a SQL query
我将 WordPress 与 WooCommerce 一起使用,我编写了以下 SQL 查询来获取产品类别术语 ID 名称和别名:
SELECT t.term_id AS id, t.name AS post_title,t.slug AS post_url
FROM wp_terms t
LEFT JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
WHERE tt.taxonomy = 'product_cat'
ORDER BY name
我应该如何更改此 SQL 查询以获取产品类别缩略图 ID?
注意: 我只对 SQL 查询感兴趣,但对 WP_Query.
等其他任何内容都不感兴趣
与其依赖 MySQL 查询,我的建议是请查看 WooCommerce 提供的 REST API。
根据您的需要,合适的人选是
Get CategoryAPI服务
其中有你期待的细节
{
"id": 9,
"name": "Clothing",
"slug": "clothing",
"parent": 0,
"description": "",
"display": "default",
"image": {
"id": 730,
"date_created": "2017-03-23T00:01:07",
"date_created_gmt": "2017-03-23T03:01:07",
"date_modified": "2017-03-23T00:01:07",
"date_modified_gmt": "2017-03-23T03:01:07",
"src": "https://example.com/wp-content/uploads/2017/03/T_2_front.jpg",
"name": "",
"alt": ""
},
"menu_order": 0,
"count": 36,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/9"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories"
}
]
}
}
对于多个类别
示例输出如下:
[
{
"id": 15,
"name": "Albums",
"slug": "albums",
"parent": 11,
"description": "",
"display": "default",
"image": [],
"menu_order": 0,
"count": 4,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/15"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories"
}
],
"up": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/11"
}
]
}
},
{
"id": 9,
"name": "Clothing",
"slug": "clothing",
"parent": 0,
"description": "",
"display": "default",
"image": {
"id": 730,
"date_created": "2017-03-23T00:01:07",
"date_created_gmt": "2017-03-23T03:01:07",
"date_modified": "2017-03-23T00:01:07",
"date_modified_gmt": "2017-03-23T03:01:07",
"src": "https://example.com/wp-content/uploads/2017/03/T_2_front.jpg",
"name": "",
"alt": ""
},
"menu_order": 0,
"count": 36,
"_links": {
"self": [
{
"href": "https://example/wp-json/wc/v3/products/categories/9"
}
],
"collection": [
{
"href": "https://example/wp-json/wc/v3/products/categories"
}
]
}
},
{
"id": 10,
"name": "Hoodies",
"slug": "hoodies",
"parent": 9,
"description": "",
"display": "default",
"image": [],
"menu_order": 0,
"count": 6,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/10"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories"
}
],
"up": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/9"
}
]
}
}
]
在 All product categories
API 请检查可用参数一些重要的参数是:
- 第
页
- per_page
- 搜索
- hide_empty
要在 SQL 查询 WooCommerce 产品类别条款时额外获得 缩略图 ID,您可以改用以下内容:
SELECT t.term_id AS id, t.name AS post_title,t.slug AS post_url, tm.meta_value AS thumb_id
FROM wp_terms t
LEFT JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
LEFT JOIN wp_termmeta tm ON t.term_id = tm.term_id
WHERE tt.taxonomy = 'product_cat'
AND tm.meta_key = 'thumbnail_id'
ORDER BY t.name
已测试并有效。
我将 WordPress 与 WooCommerce 一起使用,我编写了以下 SQL 查询来获取产品类别术语 ID 名称和别名:
SELECT t.term_id AS id, t.name AS post_title,t.slug AS post_url
FROM wp_terms t
LEFT JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
WHERE tt.taxonomy = 'product_cat'
ORDER BY name
我应该如何更改此 SQL 查询以获取产品类别缩略图 ID?
注意: 我只对 SQL 查询感兴趣,但对 WP_Query.
等其他任何内容都不感兴趣与其依赖 MySQL 查询,我的建议是请查看 WooCommerce 提供的 REST API。
根据您的需要,合适的人选是
Get CategoryAPI服务
其中有你期待的细节
{
"id": 9,
"name": "Clothing",
"slug": "clothing",
"parent": 0,
"description": "",
"display": "default",
"image": {
"id": 730,
"date_created": "2017-03-23T00:01:07",
"date_created_gmt": "2017-03-23T03:01:07",
"date_modified": "2017-03-23T00:01:07",
"date_modified_gmt": "2017-03-23T03:01:07",
"src": "https://example.com/wp-content/uploads/2017/03/T_2_front.jpg",
"name": "",
"alt": ""
},
"menu_order": 0,
"count": 36,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/9"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories"
}
]
}
}
对于多个类别
示例输出如下:
[
{
"id": 15,
"name": "Albums",
"slug": "albums",
"parent": 11,
"description": "",
"display": "default",
"image": [],
"menu_order": 0,
"count": 4,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/15"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories"
}
],
"up": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/11"
}
]
}
},
{
"id": 9,
"name": "Clothing",
"slug": "clothing",
"parent": 0,
"description": "",
"display": "default",
"image": {
"id": 730,
"date_created": "2017-03-23T00:01:07",
"date_created_gmt": "2017-03-23T03:01:07",
"date_modified": "2017-03-23T00:01:07",
"date_modified_gmt": "2017-03-23T03:01:07",
"src": "https://example.com/wp-content/uploads/2017/03/T_2_front.jpg",
"name": "",
"alt": ""
},
"menu_order": 0,
"count": 36,
"_links": {
"self": [
{
"href": "https://example/wp-json/wc/v3/products/categories/9"
}
],
"collection": [
{
"href": "https://example/wp-json/wc/v3/products/categories"
}
]
}
},
{
"id": 10,
"name": "Hoodies",
"slug": "hoodies",
"parent": 9,
"description": "",
"display": "default",
"image": [],
"menu_order": 0,
"count": 6,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/10"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories"
}
],
"up": [
{
"href": "https://example.com/wp-json/wc/v3/products/categories/9"
}
]
}
}
]
在 All product categories
API 请检查可用参数一些重要的参数是:
- 第 页
- per_page
- 搜索
- hide_empty
要在 SQL 查询 WooCommerce 产品类别条款时额外获得 缩略图 ID,您可以改用以下内容:
SELECT t.term_id AS id, t.name AS post_title,t.slug AS post_url, tm.meta_value AS thumb_id
FROM wp_terms t
LEFT JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
LEFT JOIN wp_termmeta tm ON t.term_id = tm.term_id
WHERE tt.taxonomy = 'product_cat'
AND tm.meta_key = 'thumbnail_id'
ORDER BY t.name
已测试并有效。