2015-02-23 61 views
0

,所以我查詢職位。但我有我的產品已在wooCommerce中創建的自定義屬性,但我無法在任何地方找到此信息。訪問woocomerce產品屬性使用query_posts

我試着使用:

$thePost = get_post_custom() 

我也嘗試:

$thePost = get_post_meta(get_the_ID) 

當我print_r的這些我得到了很多的信息,但我不能看到產品從woocommerce隨時隨地

屬性下面的圖片顯示瞭如何在產品上設置此信息。

enter image description here

有誰知道我如何訪問查詢後門柱的信息? 具體而言,我需要提取顏色和大小。

回答

1

產品變體保存爲另一個子帖子(自定義帖子類型product_variation)。以下代碼未經測試,但您應該明白。

query_posts('post_type=Product&showposts=-1'); 
while(have_posts()){ 
    the_post(); 
    $product_id = get_the_ID(); 

    $variations = get_posts(array('post_type' => 'product_variation', 'post_parent' => $product_id, 'posts_per_page' => -1)); 

    foreach($variations as $var){ 
    $var_customs = get_post_customs($var->ID); 
    // now you can inspect "meta" fields 
    } 
} 

所以,你必須與產品ID = 7 - >post_type=product,它的變化是post_type=product_variation&post_parent=7。尺寸和庫侖被保存爲這些變化的元值。元鍵從attribute_pa_...開始。