【问题标题】:Get also WooCommerce product category thumbnail Id using a SQL query使用 SQL 查询获取 WooCommerce 产品类别缩略图 ID
【发布时间】:2021-04-12 07:43:12
【问题描述】:

我正在将 WordPress 与 WooCommerce 一起使用,并且我编写了以下 SQL 查询,用于获取产品类别术语 ID 名称和 slug:

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 之类的其他任何东西感兴趣。

【问题讨论】:

  • 欢迎来到 StackOverflow。请参阅帮助中心(右上角圆圈中的问号),了解如何提出一个好问题。我不太确定您的要求或问题是什么。

标签: sql wordpress woocommerce custom-taxonomy taxonomy-terms


【解决方案1】:

要在您的 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

经过测试并且有效。

【讨论】:

  • 简单优雅
【解决方案2】:

我的建议是不要依赖 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"
      }
    ]
  }
}

适用于多个类别

List all product 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,请检查可用参数,一些重要参数是:

  1. 页面
  2. 每页
  3. 搜索
  4. hide_empty

【讨论】:

  • 感谢您的回答。但我的项目需要 mysql 查询
  • 好的,没问题,罗希特。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
相关资源
最近更新 更多