속성을 기반으로 WooCommerce 제품 쿼리
이건 날 미치게 만들어요특정 속성에 따라 WooCommerce 제품을 조회하여 출력하려고 합니다.예를 들어 다음과 같은 Attribute를 설정했습니다.on(가능값 포함)yes또는no.
다음을 사용하여 문의합니다.
$args = array(
'post_type' => 'product',
'meta_key' => 'pa_on',
'meta_value' => 'yes',
'posts_per_page' => -1
);
query_posts($args);
그meta_key아마 매우 중요할 것이다.on난 아무것도 얻지 못했어.라고 부르면pa_on(그렇게 해서 WooCommerce의 커스텀 속성을 이해할 수 있기 때문에) 아무것도 얻을 수 없습니다.
하지만 다른 쿼리를 시도하고_featured이것은 표준 WooCommerce 커스텀 메타싱이며, 관련된 특집 게시물을 반환합니다.누구 없어요?
오래된 버전인 것은 알지만 오늘처럼 누군가가 우연히 발견했을 경우를 대비해서 Woocommerce(v2.6.2를 사용하고 있습니다)는 이러한 커스텀 속성을 분류법으로 저장하는 것으로 보입니다.
원래 질문에 대한 올바른 arg는 다음과 같습니다.
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'pa_on',
'field' => 'name',
'terms' => 'yes'
)
)
);
인스톨에 적절한 값을 사용하고, 문제를 해결했습니다.
새로운 woocommerce의 경우:
$attribute = 'on';
$value = 'yes';
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'pa_' . $attribute,
'terms' => $value,
'field' => 'slug',
'operator' => 'IN'
)
)
);
제품 속성이 특정 제품 속성(글로벌 아님)으로 저장된 경우 분류법으로 쿼리할 수 없습니다. 대신 이 스니펫을 사용할 수 있습니다(http://snippet.fm/snippets/query-woocommerce-products-product-specific-custom-attribute/):
// Set custom attribute name and value to search for
$attribute_name = 'color';
$attribute_value = 'green';
$serialized_value = serialize( 'name' ) . serialize( $attribute_name ) . serialize( 'value' ) . serialize( $attribute_value ); // extended version: $serialized_value = serialize( $attribute_name ) . 'a:6:{' . serialize( 'name' ) . serialize( $attribute_name ) . serialize( 'value' ) . serialize( $attribute_value ) . serialize( 'position' );
$args = array(
'post_type' => 'product',
'post_status' => 'any',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_product_attributes',
'value' => $serialized_value,
'compare' => 'LIKE',
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
// do stuff here... e.g. get_the_ID()
}
wp_reset_postdata();
필요한 것은 meta_query 값이 설정된 쿼리입니다.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'pa_on',
'value' => 'yes',
'compare' => '='
)
)
);
$query = new WP_Query( $args );
상세한 것에 대하여는, http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters 를 참조해 주세요.
언급URL : https://stackoverflow.com/questions/21120916/query-woocommerce-products-based-on-attribute
'programing' 카테고리의 다른 글
| ASP.NET 디스플레이 "로드 중...". 업데이트 패널 업데이트 중 메시지 (0) | 2023.03.15 |
|---|---|
| ng-animate : 모델 변경 시 애니메이션 (0) | 2023.03.15 |
| WordPress 사용자 지정 게시 유형 아카이브-.php가 작동하지 않습니다. (0) | 2023.03.15 |
| 워드프레스에서 mysql 트랜잭션을 사용하는 방법은 무엇입니까? (0) | 2023.03.15 |
| woocommerce 제품에 콘텐츠가 없을 때 사용자 지정 탭 숨기기 (0) | 2023.03.15 |