Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Kueri SQL untuk memasukkan tag WooCommerce tertentu

Permintaan MySQL untuk mendapatkan produk dengan tag tertentu:

SELECT posts.ID AS product_id,
       posts.post_title AS product_title
FROM wp_posts AS posts,
     wp_terms AS terms,
     wp_term_relationships AS term_relationships,
     wp_term_taxonomy AS term_taxonomy
WHERE term_relationships.object_id = posts.ID
  AND term_taxonomy.term_id = terms.term_id
  AND term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id
  AND posts.post_type = 'product'
  AND posts.post_status = 'publish'
  AND term_taxonomy.taxonomy = 'product_tag'
  AND terms.slug = 'my-tag-1'; -- Replace it with your product tag

Kueri WordPress/PHP yang setara
global $wpdb;
$query = "SELECT posts.ID AS product_id,
            posts.post_title AS product_title
     FROM {$wpdb->prefix}posts AS posts,
          {$wpdb->prefix}terms AS terms,
          {$wpdb->prefix}term_relationships AS term_relationships,
          {$wpdb->prefix}term_taxonomy AS term_taxonomy
     WHERE term_relationships.object_id = posts.ID
       AND term_taxonomy.term_id = terms.term_id
       AND term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id
       AND posts.post_type = 'product'
       AND posts.post_status = 'publish'
       AND term_taxonomy.taxonomy = 'product_tag'
       AND terms.slug = 'my-tag-1';"; //Replace it with your product tag
$products = $wpdb->get_results($query);

if (!empty($products))
{
    //looping through all the products
    foreach ($products as $key => $product)
    {
        //...
        $product->product_id;
        $product->product_title;
        //...
    }
}

Kedua kueri &Kode MySQL diuji dan berfungsi.

Semoga membantu!




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MyISAM versus InnoDB

  2. BUAT tabel jenis tanggal dan gunakan nilai defaultnya sebagai CURRENT_DATE di MySQL

  3. Cara menyisipkan ke baris MYSQL dari beberapa array $_POST

  4. Kesalahan fatal:Kelas 'TableRows' tidak ditemukan di

  5. Menggunakan SSHTunelForwarder untuk terhubung ke db MySQL melalui SSH