sql >> データベース >  >> RDS >> Mysql

特定のWooCommerceタグを含めるSQLクエリ

    特定のタグで製品を取得するためのMySQLクエリ:

    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
    

    同等のWordPress/PHPクエリ
    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;
            //...
        }
    }
    

    MySQLクエリとコードの両方がテストされ、機能します。

    これがお役に立てば幸いです!




    1. パラメータをmysqlクエリにバインドする

    2. プライマリインデックスとセカンダリインデックス:パフォーマンスの違い

    3. Fetch in reactを使用して、データベースにアクセスするにはユーザー名パスワードが必要です

    4. mysqlからhtmlテーブルへのphp出力